的时候所以我在这个格式ValueError异常试图添加到字典在Python
CountryCode CountryName
USA United States
我想要做的就是用代码为重点的字典文件,以及国名定为值。
我有有这样做
def country(string):
'''reads the contents of a file into a string and closes it.'''
#open the file
countryDict = {}
fin = open(string, 'r')
for eachline in fin:
code, country = eachline.split()
countryDict[code] = country
print (countryDict)
return countryDict
然而,当我尝试运行它,我得到ValueError异常的意图的功能:值过多解压(预期2)。
为什么此代码无法正常工作的任何原因?一个类似的程序,我用这样的代码创建用户名称工作。
代码作为用户名的程序供参考,这工作,为什么不上:
def main():
print ("This program creates a file of usernames from a")
print ("file of names.")
# get the file names
infileName = input("What file are the names in? ")
outfileName = input("What file should the usernames go in? ")
# open the files
infile = open(infileName, 'r')
outfile = open(outfileName, 'w')
# process each line of the input file
for line in infile:
# get the first and last names from line
first, last = line.split()
# create a username
uname = (first[0]+last[:7]).lower()
# write it to the output file
print(uname, file=outfile)
# close both files
infile.close()
outfile.close()
print("Usernames have been written to", outfileName)
if __name__ == '__main__':
main()
http://stackoverflow.com/q/17653954/1971805不是欺骗(有点)但巧合多? – TerryA