在python中将文本文件读取到字典的问题

时间:2019-04-13 18:21:44

标签: python dictionary

嘿,每个人都有一个关于文本文件的问题,并将其放入字典中。

因此,我的代码首先通过从网站收集数据并将其写入文本文件开始。从那里我重新打开文件,并将其放入字典,以将数据从文本传输到字典。在while循环中,我得到了

的错误
 key,value = line.split()
ValueError: too many values to unpack (expected 2)

我不确定为什么我使用错误的方法将文本文件数据写入“ countryName”程序中的新位置

然后编译后,我希望能够要求用户输入一个国家名称,这将给出该国家的人均收入,如打印行所示。

def main():
    import requests
    webFile = "https://www.cia.gov/library/publications/the-world-factbook/rankorder/rawdata_2004.txt"
    data = requests.get(webFile) #connects to the file and gest a response object
    with open("capital.txt",'wb') as f:
        f.write(data.content) #write the data out to a file – wb used since thecontent from the response object is returned as abinary object.
    f.close()
    infile = open('capital.txt', 'r')
    line = infile.readline()
    countryName = {}
    while line != "":
        key,value = line.split() 
        countryName[key] = value
        line = infile.readline()
    infile.close()
    userInput = input("Enter a country name: ")
    for i in countryName:
        while(userInput != 'stop'):
            print("The per capita income in",countryName[key], "is",countryName[value])
            userInput = input("Enter a country name: ")
main()

2 个答案:

答案 0 :(得分:2)

每行的开头也都有一个数字,并且某些国家/地区名称中包含空格,从而导致split返回更长的列表。如果使用正则表达式添加分号作为定界符,并修剪前导和尾随空白,则拆分工作正常。这段代码将进入第一个while循环

line = re.sub(r"(\$)", r";\1", line) # add semicolon before $ sign
line = re.sub(r'^([0-9]+)',r'\1;', line) # add semicolon after first group of numbers
num, key, value = re.split(r';', line) # split with semicolons as delimiters
countryName[key.strip()] = value.strip() # assign key and values after stripping whitespace

答案 1 :(得分:1)

拆分返回列表,而不是字典。

a = 'a b c'
list = a.split() #-> ['a','b','c']

您是否正在尝试做类似的事情:

import requests

webFile = "https://www.cia.gov/library/publications/the-world-factbook/rankorder/rawdata_2004.txt"
data = requests.get(webFile).text #connects to the file and gest a response object
print(data)
while(1):
    name = input('Enter a country name: ')
    for a in data.splitlines():
        if name.lower() in a.lower():
            print(a.split()[-1])