UncountableSet
UncountableSet

Reputation: 35

Creating a List from Text File

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('li\n')

Upvotes: 0

Views: 1578

Answers (4)

Sefa_Kurtuldu
Sefa_Kurtuldu

Reputation: 24

   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()

Upvotes: 0

Syed Abdur Rafay
Syed Abdur Rafay

Reputation: 11

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.

Upvotes: 0

blueteeth
blueteeth

Reputation: 3565

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))

Upvotes: 2

Lukas Thaler
Lukas Thaler

Reputation: 2720

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

Upvotes: 1

Related Questions