0

我编写了一个代码以字典的形式返回信息,如下所示:

{'Mean Temp': '-0.1', 'Total Rain': '45', 'Month': '12', 'Year': '2003', 'Total Snow': '19.2', 'Max Temp': '-10.8', 'Min Temp': '9'}

{'Mean Temp': '-5.9', 'Total Rain': '34.3', 'Month': '1', 'Year': '1974', 'Total Snow': '35.6', 'Max Temp': '-20', 'Min Temp': '8.9'}

{'Mean Temp': '22.4', 'Total Rain': '20.8', 'Month': '8', 'Year': '2007', 'Total Snow': '0', 'Max Temp': '9.7', 'Min Temp': '34.8'}

{'Mean Temp': '21.7', 'Total Rain': '87.7', 'Month': '7', 'Year': '1993', 'Total Snow': '0', 'Max Temp': '11', 'Min Temp': '32.5'}

{'Mean Temp': '15.2', 'Total Rain': '112.5', 'Month': '6', 'Year': '1982', 'Total Snow': '0', 'Max Temp': '4', 'Min Temp': '25.4'}

我需要知道如何将这些单独的字典变成一个单独的列表,用逗号分隔它们。到目前为止我的代码:

    listData = weatherData.split("\n")
    i = 0
    for listData[i] in listData :
        newList = listData[i].split(",")
        i = i + 1
        newDict = dict({'Year': newList[0], 'Month': newList[1], 'Mean Temp': 
                        newList[2], 'Min Temp':newList[3], 'Max Temp':newList[4], 
                        'Total Rain': newList[5], 'Total Snow': newList[6]})
        temp = []
        dictList = []
        i = 0
        for i in newDict :
            temp = [i]
            i = i + 1
            dictList = dictList.append(temp)
            print(dictList)
4

2 回答 2

3

你快到了;您需要移出dictList = [] 循环for,并且不应将结果分配给dictlist.appendto dictlist,因为这会将变量设置为None.

您确实使您的代码过于复杂,但可以将其简化为:

dictList = []

for entry in weatherData.splitlines():
    newList = entry.split(",")
    newDict = {'Year': newList[0], 'Month': newList[1], 'Mean Temp': 
                    newList[2], 'Min Temp':newList[3], 'Max Temp':newList[4], 
                    'Total Rain': newList[5], 'Total Snow': newList[6]}
    dictList.append(newDict)

甚至:

dictList = []
fields = ('Year', 'Month', 'Mean Temp', 'Min Temp', 'Max Temp', 'Total Rain', 'Total Snow')

for entry in weatherData.splitlines():
    newList = entry.split(",")
    newDict = dict(zip(fields, newList))
    dictList.append(newDict)

我怀疑您的输入文件是 CSV 文件,在这种情况下,您可能希望使用该csv模块:

import csv

fields = ('Year', 'Month', 'Mean Temp', 'Min Temp', 'Max Temp', 'Total Rain', 'Total Snow')

with open(yourweatherdatafile, 'rb') as weather:
    reader = csv.DictReader(weather, fields=fields)
    dictList = list(reader)
于 2013-04-04T18:17:15.320 回答
1

ast.literal_eval是你的朋友:

weatherData = """
{'Mean Temp': '-0.1', 'Total Rain': '45', 'Month': '12', 'Year': '2003', 'Total Snow': '19.2', 'Max Temp': '-10.8', 'Min Temp': '9'}

{'Mean Temp': '-5.9', 'Total Rain': '34.3', 'Month': '1', 'Year': '1974', 'Total Snow': '35.6', 'Max Temp': '-20', 'Min Temp': '8.9'}

{'Mean Temp': '22.4', 'Total Rain': '20.8', 'Month': '8', 'Year': '2007', 'Total Snow': '0', 'Max Temp': '9.7', 'Min Temp': '34.8'}

{'Mean Temp': '21.7', 'Total Rain': '87.7', 'Month': '7', 'Year': '1993', 'Total Snow': '0', 'Max Temp': '11', 'Min Temp': '32.5'}

{'Mean Temp': '15.2', 'Total Rain': '112.5', 'Month': '6', 'Year': '1982', 'Total Snow': '0', 'Max Temp': '4', 'Min Temp': '25.4'}

 """
import ast
list_data = weatherData.splitlines()
list_of_dict = [ast.literal_eval(line) for line in list_data if line.strip()]

如果您更喜欢,您也可以使用json.loads(line)而不是。ast.literal_eval(line)

于 2013-04-04T18:15:23.530 回答