简体   繁体   English

从文本文件创建列表

[英]Creating a List from Text File

Question问题

Create a list called destination using the data stored in travel_plans.txt.使用存储在 travel_plans.txt 中的数据创建一个名为 destination 的列表。 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旅行计划.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')

Your for loop will not work like this.您的 for 循环不会像这样工作。 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.相反,他们会像在England: London一样与一个国家保持联系England: London幸运的是,你不需要那个。 A for loop like像一个 for 循环

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 if条件检查行中是否有冒号,在这种情况下,根据描述,该行将是所需的行之一,并相应地将其附加到列表中

You're pretty close.你很接近。

  • It's better to use a context manager when dealing with files.处理文件时最好使用上下文管理器。
  • You don't need a second for loop.你不需要第二个for循环。

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) .在我看来,通过将国家和城市分成(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.然后简单地 append 它到目的地列表。 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM