尝试在Python中添加到字典时的ValueError

时间:2013-07-17 06:32:22

标签: python list file-io dictionary

所以我有一个这种格式的文件

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

2 个答案:

答案 0 :(得分:4)

考虑line的时间:

USA           United States

当你拆分它时,它会创建:

['USA', 'United', 'States']

当你去做first, last = line.split()时,它会尝试将三个值放入两个变量中(因此错误)。

为防止这种情况,您可以拆分一次:

>>> first, last = 'USA           United States'.split(None, 1)
>>> first
'USA'
>>> last
'United States'

答案 1 :(得分:0)

使用正则表达式的另一种方法

def country(string):
    fin = open(string, 'r')
    pat = r'\s*([A-Za-z0-9]*)\s+([A-Za-z0-9\s]*?)\n'
    tup = re.findall(pat, fin)
    return dict(tup)