2012-11-14 15 views
5

मुझे दुनिया के सभी देशों की एक txt फ़ाइल मिली है और वे किस प्रकार के उत्पाद निर्यात करते हैं। [Jamaica\t alumina, bauxite, sugar, rum, coffee, yams, beverages, chemicals, wearing apparel, mineral fuels\n]मैं कुछ और फिर इसकी सूची कैसे प्रिंट करूं?

मैं एक प्रोग्राम है जो करता है लिखने के लिए है:

यह एक पंक्ति किसी भी बंटवारे या अलग करना (नोटिस \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'] 

यह क्या है मैं अब तक किया गया है लेकिन अब से मुझे नहीं पता कि आगे कैसे जाना है:

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

'आयात ओएस' अनावश्यक है, जब तक कि आप इसे अपने कोड के एक अलग भाग (ऊपर दिखाया नहीं गया) में उपयोग नहीं कर रहे हैं। साथ ही, उस मामले के लिए अपने चर 'फ़ाइल' (या 'सूची' या' dict', आदि) का नाम देना एक बुरा विचार है) – inspectorG4dget

+0

'seznam' क्या है? – kojiro

+0

क्षमा करें, संपादित करें :) – user1824179

उत्तर

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 

आशा करना चाहिए इस में मदद करता है

+0

मुझे लगता है कि आप निर्यात की सूची कहां से एक ब्रैकेट खो रहे हैं। – thegrinner

+0

@thegrinner: आप बिल्कुल सही हैं। बग रिपोर्ट के लिए धन्यवाद। फिक्स्ड – inspectorG4dget

+0

लाइन को अनावश्यक रूप से दो बार विभाजित करता है। मैं 'key, value = line.split ('\ t', 1) का उपयोग करूंगा; निर्यात [कुंजी] = मूल्य'। – glglgl

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

हां, लेकिन फिर मुझे मिलता है: यूनाइटेड ['किंगडम \ टी', 'निर्मित', 'सामान,', 'ईंधन,', 'रसायन,', 'भोजन, ',' पेय पदार्थ, ',' तंबाकू \ n '] – user1824179

संबंधित मुद्दे