Python - CSV到嵌套字典

时间:2012-06-19 13:45:27

标签: python csv

我需要导入包含此类数据的CSV文件

                       January February March
              Amy      4     3      4
              Ben      5     2      0 
              Clara    0     3.5    4  

Top Row包含月份(名称),第一列包含名称(销售额)和月份预订。

寻找一种方法来获得像这样的字典输出,它需要删除0

的月份
                {"Amy": {"January ": 4, "February": 3, "March": 4},
                  "Ben": {"January": 5, "February": 2},
                  "Clara": {"February": 3.5, "March": 4}}

任何帮助将不胜感激。

谢谢,

的Rahul

3 个答案:

答案 0 :(得分:2)

让我们假设您如何从csv获取数据到变量,检查唯一名称,无效数据......,并且您正在努力排除0值:

更新:正在阅读我的代码中包含的csv文件:

# Python 3.2
import csv
delimiter = ','
result = {}
with open("data.csv", 'r') as data_file:
    data = csv.reader(data_file, delimiter=delimiter)
    headers = next(data)[1:] # month names starting from 2nd column in csv
    for row in data:
        temp_dict = {}
        name = row[0]
        values = []
        # converting each value from string to int / float
        # (as suggested by OP's example)
        for x in row[1:]:
            try:
                values.append(int(x))
            except ValueError:
                try:
                    values.append(float(x))
                except ValueError:
                    print("Skipping value '{}' that cannot be converted " +
                          "to a number - see following row: {}"
                          .format(x, delimiter.join(row)))
                    values.append(0)
        for i in range(len(values)):
            if values[i]: # exclude 0 values
                temp_dict[headers[i]] = values[i]
        result[name] = temp_dict    
print(result)

答案 1 :(得分:0)

如果您知道CSV的分隔符,则可以执行以下操作:

tokens = line.split(delimiter)
tokens = [token.strip() for token in tokens]

...对于CSV的每一行。您需要从解析的第一行中提取适当的值以获取内部词典的键。之后,要获取最外层词典的密钥,您可以使用tokens[0],依此类推。

现在,话说回来,你的CSV看起来更像是一个固定宽度的文件而不是CSV。

答案 2 :(得分:0)

import csv
ifile = open('test.csv', "rb")
reader = csv.reader(ifile)
rownum = 0
td = {'j': 0, 'f': 0, 'm': 0}
d = {}
for row in reader:
    # Save header row.
    if rownum == 0:
        header = row
    else:
        for c, col in enumerate(row):
            if c != 0:
                td[header] = col
        d[row[0]] = td