Creating a List from Text File
Question:
Question
Create a list called destination using the data stored in travel_plans.txt. Each element of the list should contain a line from the file that lists a country and cities inside that country. Hint: each line that has this information also has a colon : in it.
travel_plans.txt
This summer I will be travelling
I will go to...
Italy: Rome
Greece: Athens
England: London, Manchester
France: Paris, Nice, Lyon
Spain: Madrid, Barcelona, Granada
Austria: Vienna
I will probably not even want to come back!
However, I wonder how I will get by with all the different languages.
I only know English!
My Code so Far
file=open('travel_plans.txt','r')
lines=file.readlines()
destination=[]
for li in lines:
val=li.strip().split(',')
for j in val:
if j==":":
destination.append('lin')
Answers:
Your for loop will not work like this. If you split by commas, the colons will never end up in an element of their own. Instead, they’ll stay attached to a country as in England: London
Luckily, you don’t need that. A for loop like
for li in lines:
if ':' in li:
destination.append(li)
will do the trick for you. The if
condition checks if there is a colon in the line, in which case, according to the description, the line will be one of the wanted ones and append it to the list accordingly
You’re pretty close.
- It’s better to use a context manager when dealing with files.
- You don’t need a second
for
loop.
Here’s what I would do:
with open('travel_plans.txt') as f:
for line in f:
if ':' in line:
destinations.append(line)
You could make this slightly better in my opinion, by separating the country and cities into a tuple of (country, cities)
.
with open('travel_plans.txt') as f:
for line in f:
if ':' in line:
country, cities = line.split(':', 1)
cities = [city.strip() for city in cities.split(',')]
destinations.append((country, cities))
their is also a simpler way to answer this question
with open("travel_plans.txt","r") as tf:
travel=tf.readlines()
destination=[]
for line in travel:
if ":" in line:
destination.append(line)
you can simply iterate through lines in travel and check if colon ":" is present in any of the lines if it is present then it would be a destination we are looking for. Then simply append it to a destination list.
Hope this helps thanks.
f = open("travel_plans.txt", "r")
for aline in f:
destination = aline.split()
destination = "".join(destination)
for char in destination:
if char == ":":
last_destinion = destination.split()
print(last_destinion)
f.close()
Question
Create a list called destination using the data stored in travel_plans.txt. Each element of the list should contain a line from the file that lists a country and cities inside that country. Hint: each line that has this information also has a colon : in it.
travel_plans.txt
This summer I will be travelling
I will go to...
Italy: Rome
Greece: Athens
England: London, Manchester
France: Paris, Nice, Lyon
Spain: Madrid, Barcelona, Granada
Austria: Vienna
I will probably not even want to come back!
However, I wonder how I will get by with all the different languages.
I only know English!
My Code so Far
file=open('travel_plans.txt','r')
lines=file.readlines()
destination=[]
for li in lines:
val=li.strip().split(',')
for j in val:
if j==":":
destination.append('lin')
Your for loop will not work like this. If you split by commas, the colons will never end up in an element of their own. Instead, they’ll stay attached to a country as in England: London
Luckily, you don’t need that. A for loop like
for li in lines:
if ':' in li:
destination.append(li)
will do the trick for you. The if
condition checks if there is a colon in the line, in which case, according to the description, the line will be one of the wanted ones and append it to the list accordingly
You’re pretty close.
- It’s better to use a context manager when dealing with files.
- You don’t need a second
for
loop.
Here’s what I would do:
with open('travel_plans.txt') as f:
for line in f:
if ':' in line:
destinations.append(line)
You could make this slightly better in my opinion, by separating the country and cities into a tuple of (country, cities)
.
with open('travel_plans.txt') as f:
for line in f:
if ':' in line:
country, cities = line.split(':', 1)
cities = [city.strip() for city in cities.split(',')]
destinations.append((country, cities))
their is also a simpler way to answer this question
with open("travel_plans.txt","r") as tf:
travel=tf.readlines()
destination=[]
for line in travel:
if ":" in line:
destination.append(line)
you can simply iterate through lines in travel and check if colon ":" is present in any of the lines if it is present then it would be a destination we are looking for. Then simply append it to a destination list.
Hope this helps thanks.
f = open("travel_plans.txt", "r")
for aline in f:
destination = aline.split()
destination = "".join(destination)
for char in destination:
if char == ":":
last_destinion = destination.split()
print(last_destinion)
f.close()