我正在尝试插入从日期列中获取的数据。列标题在csv文件中为date,day,month,year,pcp1,pcp2,pcp3,pcp4,pcp5,pcp6
。列day, month, year
目前为空。
我想将通过拆分方法从日期获得的数据插入到这些列中。我怎么能这样做?
以下是csv文件中的示例数据:
date day month year pcp1 pcp2 pcp3 pcp4 pcp5 pcp6
1.01.1979 0.431 2.167 9.375 0.431 2.167 9.375
2.01.1979 1.216 2.583 9.162 1.216 2.583 9.162
3.01.1979 4.041 9.373 23.169 4.041 9.373 23.169
4.01.1979 1.799 3.866 8.286 1.799 3.866 8.286
5.01.1979 0.003 0.051 0.342 0.003 0.051 0.342
6.01.1979 2.345 3.777 7.483 2.345 3.777 7.483
7.01.1979 0.017 0.031 0.173 0.017 0.031 0.173
这是我的代码:
import csv
dd=[]
mm=[]
yy=[]
with open('output2.csv') as csvfile:
reader = csv.DictReader(csvfile, fieldnames=("date","day","month","year","pcp1","pcp2","pcp3","pcp4","pcp5","pcp6"), delimiter=';', quotechar='|')
next(reader) # skip header row
x = [row['date'] for row in reader]
for date_str in x:
day, month, year = date_str.split('.')
dd.append(day)
mm.append(month)
yy.append(year)
csvfile.close()
with open('output2.csv') as f:
fieldnames = ["date","day","month","year","pcp1","pcp2","pcp3","pcp4","pcp5","pcp6"]
writer = csv.DictWriter(f, fieldnames=fieldnames,delimiter=';', quotechar='|')
for i in range(len(dd)):
writer.writerow({'day':dd[i]})
for i in range(len(mm)):
writer.writerow({'month':mm[i]})
for i in range(len(yy)):
writer.writerow({'year':yy[i]})
f.close()
答案 0 :(得分:2)
您可以按如下方式解析CSV。这会将所有行读入列表,然后将日期组件插入空列。
import csv
with open('output2.csv', newline='') as f_input:
csv_input = csv.reader(f_input, delimiter=';', quotechar='|')
header = next(csv_input)
rows = list(csv_input)
with open('output2b.csv', 'w', newline='') as f_output:
csv_output = csv.writer(f_output, delimiter=';', quotechar='|')
csv_output.writerow(header)
for row in rows:
day, month, year = row[0].split('.')
row[1:4] = [day, month, year]
csv_output.writerow(row)
给你以下输出:
date;day;month;year;pcp1;pcp2;pcp3;pcp4;pcp5;pcp6
1.01.1979;1;01;1979;0.431;2.167;9.375;0.431;2.167;9.375
2.01.1979;2;01;1979;1.216;2.583;9.162;1.216;2.583;9.162
3.01.1979;3;01;1979;4.041;9.373;23.169;4.041;9.373;23.169
4.01.1979;4;01;1979;1.799;3.866;8.286;1.799;3.866;8.286
5.01.1979;5;01;1979;0.003;0.051;0.342;0.003;0.051;0.342
6.01.1979;6;01;1979;2.345;3.777;7.483;2.345;3.777;7.483
7.01.1979;7;01;1979;0.017;0.031;0.173;0.017;0.031;0.173
使用Python 3.5.2进行测试
答案 1 :(得分:0)
使用熊猫。您将能够使用距离工作不太远的大部分代码
import pandas as pd
filename = "test.csv"
data = pd.read_excel(filename)
x = data["date"]
dd=[]
mm=[]
yy=[]
for date_str in x:
day, month, year = date_str.split('.')
dd.append(day)
mm.append(month)
yy.append(year)
data["day"] = dd
data["month"] = mm
data["year"] = yy
data.to_csv("test2.csv")
在test2.csv中,日月份已填满。如果你不想把索引作为第一行(我不会)使用
data.to_csv("test2.csv",index = False)
作为最后一道