[英]Take 1st row of CSV and turn it into a default value in a new column
I have a fussy file export that basically creates a CSV with an odd format like:我有一个繁琐的文件导出,它基本上创建了一个带有奇怪格式的 CSV,例如:
filename, date
header1, header2, header3, header4
value1, value2, value3, value4
value5, value6, value7, value8
I'm trying to take that 'date' field from the first row and create a 5th column with that value as the default value.我正在尝试从第一行中获取该“日期”字段,并以该值作为默认值创建第 5 列。
I then want to delete that first row entirely.然后我想完全删除第一行。 Deleting the row isn't the issue, I just can't think of a way of selecting that date value and turning it into a new column.
删除行不是问题,我只是想不出一种选择该日期值并将其转换为新列的方法。
You'll use the csv library to read like so:您将使用 csv 库读取如下内容:
import csv
with open('path_to_file_', 'r') as fh:
reader = csv.reader(fh)
# first get the date
first_line = next(reader)
date = first_line[1]
# read the rest and add the date to every line
lines = [l.append(date) for l in list(reader)]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.