简体   繁体   English

如何打印某些内容然后列出其中的内容?

[英]How do I print something and then its list?

I've got a txt file of all the countries in the world and what kind of products do they export. 我有一个世界上所有国家的txt文件,他们出口的是什么类型的产品。

This is what one line looks like without any splitting or stripping (notice \\t and \\n ): [Jamaica\\t alumina, bauxite, sugar, rum, coffee, yams, beverages, chemicals, wearing apparel, mineral fuels\\n] 这是一条线看起来没有任何分裂或剥离(注意\\t\\n ): [Jamaica\\t alumina, bauxite, sugar, rum, coffee, yams, beverages, chemicals, wearing apparel, mineral fuels\\n]

I have to write a program which does that: 我必须写一个程序来做到这一点:

Angola
[ 'oil,', 'diamonds,', 'refined', 'petroleum', 'products,', 'coffee,', 'sisal,', 'fish,', 'fish', 'products,', 'timber,', 'cotton']

Anguilla
[ 'lobster,', 'fish,', 'livestock,', 'salt,', 'concrete', 'blocks,', 'rum']

Antigua and Barbuda
[ 'petroleum', 'products,', 'bedding,', 'handicrafts,', 'electronic', 'components,', 'transport', 'equipment,', 'food,', 'live', 'animals']

Argentina
[ 'soybeans,', 'petroleum,', 'gas,', 'vehicles,', 'corn,', 'wheat']

This is what I've done so far but from now on I don't know how to go forward: 这是我迄今为止所做的,但从现在开始,我不知道如何前进:

import os
file=open("exports.txt",'r')
list=[]

for i in file:
    list.append(i.split(" "))

for i in range(len(list)):
    print(list[i])

As a result I get a list of every country and what does it export: 结果我得到了每个国家的清单以及它的出口情况:

['Angola\t', 'oil,', 'diamonds,', 'refined', 'petroleum', 'products,', 'coffee,', 'sisal,', 'fish,', 'fish', 'products,', 'timber,', 'cotton\n']
['Anguilla\t', 'lobster,', 'fish,', 'livestock,', 'salt,', 'concrete', 'blocks,', 'rum\n']
['Antigua', 'and', 'Barbuda\t', 'petroleum', 'products,', 'bedding,', 'handicrafts,', 'electronic', 'components,', 'transport', 'equipment,', 'food,', 'live', 'animals\n']
['Argentina\t', 'soybeans,', 'petroleum,', 'gas,', 'vehicles,', 'corn,', 'wheat\n']

How do I countinue? 我怎么算? Thanks for help 感谢帮助

This should do it 这应该做到这一点

with open("exports.txt",'r') as infile:
    exports = {}
    for line in infile:
        parts = line.partition('\t')
        exports[parts[0]] = parts[-1].strip().split(', ')

for country, exports in exports.iteritems():
    print country
    print exports

Hope this helps 希望这可以帮助

Assuming you have already got the list you mentioned, you can simply do 假设你已经得到了你提到的清单,你就可以做到

>>> some_list
[['Angola\t', 'oil,', 'diamonds,', 'refined', 'petroleum', 'products,', 'coffee,', 'sisal,', 'fish,', 'fish', 'products,', 'timber,', 'cotton\n'], ['Anguilla\t', 'lobster,', 'fish,', 'livestock,', 'salt,', 'concrete', 'blocks,', 'rum\n'], ['Antigua', 'and', 'Barbuda\t', 'petroleum', 'products,', 'bedding,', 'handicrafts,', 'electronic', 'components,', 'transport', 'equipment,', 'food,', 'live', 'animals\n'], ['Argentina\t', 'soybeans,', 'petroleum,', 'gas,', 'vehicles,', 'corn,', 'wheat\n']]
>>> for row in some_list:
    print row[0]
    print map(str.strip,row[1:])


Angola  
['oil,', 'diamonds,', 'refined', 'petroleum', 'products,', 'coffee,', 'sisal,', 'fish,', 'fish', 'products,', 'timber,', 'cotton\n']
Anguilla    
['lobster,', 'fish,', 'livestock,', 'salt,', 'concrete', 'blocks,', 'rum\n']
Antigua
['and', 'Barbuda\t', 'petroleum', 'products,', 'bedding,', 'handicrafts,', 'electronic', 'components,', 'transport', 'equipment,', 'food,', 'live', 'animals\n']
Argentina   
['soybeans,', 'petroleum,', 'gas,', 'vehicles,', 'corn,', 'wheat\n']
>>> 

When you're iterating through the list from the file, you can use list.pop(0) - this will give you the first element of the list and remove it from the list. 当您从文件中迭代列表时,可以使用list.pop(0) - 这将为您提供列表的第一个元素并将其从列表中删除。

I'd also recommend switching to using the with keyword for opening the file, and changing your variable name. 我还建议切换到使用with关键字打开文件,并更改变量名称。 So something like: 所以类似于:

with open("exports.txt",'r') as infile:
    lines = infile.readlines()

for line in lines: 
    print line.pop(0) #Note that this doesn't actually remove the tab
    print line

My advice: construct a dict mapping the country to the things it produces, and split each line on the "\\t" 我的建议:构建一个将国家映射到它产生的东西的字典,并将每一行分成“\\ t”

file=open("exports.txt",'r')
dict = {}

for i in file:
    spl_line = i.split("\t")
    dict[spl_line[0]] = spl_line[1].split(" ")

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

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