读取csv文件时如何创建字典?

时间:2019-04-24 13:50:22

标签: python python-3.x

我有以下蛮力代码:

import csv
d = {}
with open(csv_filename, 'r') as csv_file:
    reader = csv.reader(csv_file)
    for row in reader:
        key = row[0]
        d[key] = np.array(
            [[float(x) for x in row[1:4]], [float(x) for x in row[4:7]],
             [float(x) for x in row[7:]]], dtype=float)

是否有一种方法可以重写它以避免[1:4], [4:7]等?

1 个答案:

答案 0 :(得分:1)

如果我理解正确,则只需要重塑数据即可。

{"nr": "test_nr", "epoch_timestamp":"1557114198", "value":[1200,2300,5400,1200]} and so on..