2012-11-14 34 views
5

私は世界のすべての国のtxtファイルを持っています。どのような種類の製品を輸出していますか? [Jamaica\t alumina, bauxite, sugar, rum, coffee, yams, beverages, chemicals, wearing apparel, mineral fuels\n]何かを印刷してそのリストを印刷するには?

私が行うプログラムを記述する必要があり:

は、これは、1つの行が分割またはストリッピング(予告\t\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'] 

これは何をであるI今まで行ったことがありますが、今から私はどのように進むべきか分かりません:

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

私はカウントダウンしますか?これはあなたの最初の要素を与えるだろう - ヘルプ

+3

ここでは、コードの別の部分(上記には示していません)で使用していない限り、ここではimport osは不要です。また、変数に 'file'(または' list'や 'dict'など)という名前を付けるのは悪い考えです。 – inspectorG4dget

+0

' seznam'とは何ですか? – kojiro

+0

申し訳ありません、編集済み: – user1824179

答えて

6

ため おかげでこれを使用すると、ファイルからリストを反復処理しているとき、これはlist.pop(0)、あなたが使用することができます

+0

私はあなたが輸出のリストを作成する場所から括弧が不足していると思います。 – thegrinner

+0

@thegrinner:あなたは絶対に正しいです。バグレポートに感謝します。固定 – inspectorG4dget

+0

不必要に2回線を分割します。私は 'key、value = line.split( '\ t'、1);を使用します。輸出[キー] =値 '。 – glglgl

0

を助け、それを

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 

希望を行う必要がありますそれをリストから削除します。

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 
0

私のアドバイスは:それが生成するものに国をマッピングする辞書を構築し、すでにリストを持っていると仮定すると、「\ tの」

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

for i in file: 
    spl_line = i.split("\t") 
    dict[spl_line[0]] = spl_line[1].split(" ") 
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'] 
>>> 
+0

はい、でも私は得意です:United Kingdom ['Kingdom \ t'、 '製造'、 '商品'、 '燃料'、 '化学品'、 ' '、'飲み物 '、'タバコ\ n '] – user1824179

関連する問題