0
私はCSVへのエクスポートを行っていましたが、フィールド名に問題があります。 2017は全て365分の12個のセルの上のセル、または各月(月用をマージするように、私は、どのように関節に細胞を知らないので、私は「フィールド名」python csv fieldnames error
Row 1 : Field 1:empty ; Field 2:"YEAR" , Field3:"2017"
Row 2 : Field 1:empty ; Field 2:"MONTH", Field3: month.number
Row 3 : Field 1:"STATION"; Field 2:"CODE" , Field3: day.number
の3行をしたいです私は何を得るのプログラムを実行した後など
import csv
import calendar
from datetime import datetime
HYear= [' ','Year','2017']
HMonth=[' ','Month','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug',
'Sep','Oct','Nov','Dec']
HDay= ['Station','Code','01','02','03','04','05','06','07',
'08','09','10','11','12','13','14','15','16','17','18',
'19','20','21','22','23','24','25','26','27','28','29','30','31']
#VARs
today = datetime.today()
num_day = today.day
num_month = today.month
mon1 = calendar.monthrange(2017, 1)[1] ###31
mon2 = calendar.monthrange(2017, 2)[1]+mon1###59
mon3 = calendar.monthrange(2017, 3)[1]+mon2###90
mon4 = calendar.monthrange(2017, 4)[1]+mon3
mon5 = calendar.monthrange(2017, 5)[1]+mon4
mon6 = calendar.monthrange(2017, 6)[1]+mon5
mon7 = calendar.monthrange(2017, 7)[1]+mon6
mon8 = calendar.monthrange(2017, 8)[1]+mon7
mon9 = calendar.monthrange(2017, 9)[1]+mon8
mon10= calendar.monthrange(2017,10)[1]+mon9
mon11= calendar.monthrange(2017,11)[1]+mon10
mon12= calendar.monthrange(2017,12)[1]+mon11
#Days in Year
for i in range(1,num_month):
d = calendar.monthrange(2017, i)[1]
num_day+=sum((0, d))
days_year = num_day###135 or so
#to CSV
def csv_dict_writer(path, fieldnames, data):
with open(path, "wb") as out_file:
writer = csv.DictWriter(out_file, delimiter=',', fieldnames=fieldnames)
writer.writeheader()
for row in data:
writer.writerow(row)
#####Headers
heady="%s,%s"%(HYear[0],HYear[1])
headm="%s,%s"%(HMonth[0],HMonth[1])
headd="%s,%s"%(HDay[0],HDay[1])
#####Adding more content to the headers
for i in range(2,days_year+2):###2-367
if i>1:
heady+=",%s"%HYear[2]
if i in range(2,mon1+2):###2-33
headm+=",%s"%HMonth[2]
headd+=",%s"%HDay[i]
elif i in range(mon1+2,mon2+2):###33-61
headm+=",%s"%HMonth[3]
headd+=",%s"%HDay[i-mon1]
elif i in range(mon2 +2,mon3 +2):
headm+=",%s"%HMonth[4]
headd+=",%s"%HDay[i-mon2]
elif i in range(mon3 +2,mon4 +2):
headm+=",%s"%HMonth[5]
headd+=",%s"%HDay[i-mon3]
elif i in range(mon4 +2,mon5 +2):
headm+=",%s"%HMonth[6]
headd+=",%s"%HDay[i-mon4]
elif i in range(mon5 +2,mon6 +2):
headm+=",%s"%HMonth[7]
headd+=",%s"%HDay[i-mon5]
elif i in range(mon6 +2,mon7 +2):
headm+=",%s"%HMonth[8]
headd+=",%s"%HDay[i-mon6]
elif i in range(mon7 +2,mon8 +2):
headm+=",%s"%HMonth[9]
headd+=",%s"%HDay[i-mon7]
elif i in range(mon8 +2,mon9 +2):
headm+=",%s"%HMonth[10]
headd+=",%s"%HDay[i-mon8]
elif i in range(mon9 +2,mon10+2):
headm+=",%s"%HMonth[11]
headd+=",%s"%HDay[i-mon9]
elif i in range(mon10+2,mon11+2):
headm+=",%s"%HMonth[12]
headd+=",%s"%HDay[i-mon10]
elif i in range(mon10+2,mon12+2):
headm+=",%s"%HMonth[13]
headd+=",%s"%HDay[i-mon11]
print heady
print headm
print headd
###Creating CSV
if __name__ == "__main__":
data=[heady.split(","),
headm.split(","),
headd.split(",")]
my_list = []
fieldnames = data[0]
for values in data[1:]:
inner_dict = dict(zip(fieldnames, values))
my_list.append(inner_dict)
path = "dict_output.csv"
csv_dict_writer(path, fieldnames, my_list)
例)は日の上に31個のセルをマージし...私は2017年または月(31回) 2月(各セルを印刷してる28回)変数heady、headm、およびheaddは次のとおりです。(それぞれを出力)
#Print Header Year
,Year,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017
#Print Header Month
,Month,Jan,Jan,Jan,Jan,Jan,Jan,Jan,Jan,Jan,Jan,Jan,Jan,Jan,Jan,Jan,Jan,Jan,Jan,Jan,Jan,Jan,Jan,Jan,Jan,Jan,Jan,Jan,Jan,Jan,Jan,Jan,Feb,Feb,Feb,Feb,Feb,Feb,Feb,Feb,Feb,Feb,Feb,Feb,Feb,Feb,Feb,Feb,Feb,Feb,Feb,Feb,Feb,Feb,Feb,Feb,Feb,Feb,Feb,Feb,Mar,Mar,Mar,Mar,Mar,Mar,Mar,Mar,Mar,Mar,Mar,Mar,Mar,Mar,Mar,Mar,Mar,Mar,Mar,Mar,Mar,Mar,Mar,Mar,Mar,Mar,Mar,Mar,Mar,Mar,Mar,Apr,Apr,Apr,Apr,Apr,Apr,Apr,Apr,Apr,Apr,Apr,Apr,Apr,Apr,Apr,Apr,Apr,Apr,Apr,Apr,Apr,Apr,Apr,Apr,Apr,Apr,Apr,Apr,Apr,Apr,May,May,May,May,May,May,May,May,May,May,May,May,May,May,May
#Print Header Day
Station,Code,01,02,03,04,05,06,07,08,09,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,01,02,03,04,05,06,07,08,09,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,01,02,03,04,05,06,07,08,09,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,01,02,03,04,05,06,07,08,09,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,01,02,03,04,05,06,07,08,09,10,11,12,13,14,15
しかし、私はCSVファイルにヘッダーとして取得することです:
Row 1: empty, Year, 2017, 2017 ... (correct)
Row 2: Empty, Month, May, May ... (should be Jan not May)
Row 3-360+: Station, Code, 15, 15 ... (should be 01, 02, ..,31, 01, 02... and so on)
どんな提案? ありがとうございます!
PS:出力は
リストを取得し、あなたのファイルに書き込むためにあなたのcsvライター・メソッドとメインを変更empty, YEAR, 2017, 2017, 2017....*365
empty, MONTH, Jan, Jan, Jan...*31, Feb, Feb...*28.....
STATION, CODE, 01, 02, 03..., 31, 01, 02,...,28, 01.....
感謝の男、CSVは、今、私が望んでいたが、彼らは3列に印刷されたデータを持っていますが、データは、それを変更する方法を、3行のヘッダのですか? – gilgameshbk
サンプル出力を共有することができますか? –
Row1とstuff ..を言及せずに質問を編集できますか?それは期待される内容のみを含みます。 –