如何写入CSV文档

时间:2017-04-29 12:48:43

标签: python csv

我正在尝试写一个CSV文档。我需要打开一个CSV文档,取行created_at(日期和时间)并打破两行Dia(Day)和Hora(Hour),但它不是写作。

错误:

Traceback (most recent call last):  
  File "C:\Teste\teste.py", line 30, in <module>     
    writer.writerows(dia,hora) 
TypeError: writerows() takes exactly 2 arguments (3 given) 

代码:

import csv 
import os

fin = open('teste.csv', 'r')  
fout = open('teste2.csv', 'w')

reader = csv.DictReader(fin, delimiter=',') writer = csv.DictWriter(fout, delimiter=',', quotechar='"', quoting=csv.QUOTE_ALL,fieldnames=reader.fieldnames)  

writer.writeheader() 

with open(os.path.join('teste2.csv'), 'wb') as csvfile:     

    writer = csv.DictWriter(csvfile, fieldnames = ['dia','hora'], delimiter = ',')     
    writer.writeheader()    

for row in reader:      
    arquivo = row['created_at'].split("T")[0]      
    data = arquivo.split("T")[0]     
    dia = data.split("-")[2]      
    horario = row['created_at'].split("T")[1]     
    hora= horario.split(":")[0]     
    writer.writerows(dia,hora) 

fin.close() 
fout.close() 

0 个答案:

没有答案