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

时间:2012-11-14 15:53:28

标签: python

我有一个世界上所有国家/地区的txt文件以及他们导出的产品类型。

这是一行没有任何拆分或剥离的情况(注意\t\n): [Jamaica\t alumina, bauxite, sugar, rum, coffee, yams, beverages, chemicals, wearing apparel, mineral fuels\n]

我必须写一个程序来做到这一点:

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']

这是我迄今为止所做的,但从现在起我不知道如何前进:

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

因此,我得到了每个国家/地区的列表以及它的导出内容:

['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']

我如何计算? 谢谢你的帮助

4 个答案:

答案 0 :(得分:6)

这应该这样做

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

希望这有帮助

答案 1 :(得分:1)

假设您已经获得了您提到的列表,您可以直接执行

>>> 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']
>>> 

答案 2 :(得分:0)

当您从文件中迭代列表时,可以使用list.pop(0) - 这将为您提供列表的第一个元素并将其从列表中删除。

我还建议您切换到使用with关键字打开文件,并更改变量名称。如下所示:

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

答案 3 :(得分:0)

我的建议:构建一个dict,将国家映射到它产生的东西,并将每一行分成“\ t”

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

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