CSV Dict编写器格式

时间:2018-09-23 16:17:39

标签: python arrays dictionary

我在格式化文件时遇到问题。我希望每个标头都与其对应的值相关联。我还需要访问专门用于数据转换的每个值(索引)。

非常感谢您的帮助。

class CsvReader:

read_path = './data/samples.dat'
with open(read_path, newline='') as File, open('./data/samples_curated.csv', 'w+') as write_path:
    reader = csv.reader(File, delimiter="|")

    for row in reader:
        next(File)

        write_path.write('\n')
        fnames = ['Sequence n', 'Year', 'Month', 'Day', 'Hour', 'Minute', 'Second', 'Speed', 'Acc Foward',
                  'Acc Backward', 'Acc left', 'Acc right', 'Acc up', 'Acc down', 'E/W Cord', 'E/W',
                  'N/S Cord', 'N/S', 'Engine RMP', 'Voltage', 'Altitude Above sea Level', 'Driver ID 1',
                  'Driver ID 2', 'Driver ID 3', 'Driver ID 4', 'Binary Digital Input', 'Engine Temprature'
                  ]
        writer = csv.DictWriter(write_path, fieldnames=fnames)
        writer.writeheader()
        writer.writerow({'Sequence n': row, 'Year': row})

0 个答案:

没有答案