文件.txt
Harry Moon, Meteor \n roger gim, astronaut \n john banks, westpac \n john banks, ASB
代码:
FILE=file.txt
filename=open(FILE,"rt")
fileread=filename.readlines()
fileread.sort()
for i in fileread:
dict={}
dict[i.split(',')[0]]=i.split(',')[1]
print dict
有了这个,我能够获得以下内容:
{'Harry Moon': ' Meteor\n'}
{'john banks': ' ASB'}
{'john banks': ' westpac\n'}
{'roger gim': ' astronaut\n'}
但是,我想要的是这样的:
{ 'Harry Moon': ' Meteor'
, 'john banks': ' ASB'
, 'john banks': ' westpac'
, 'roger gim': 'astronaut' }