I need my script to write to a CSV file. The same will open a CSV file that has the column created_at
(date and time), after that, the variable will be "broken" and will be written in a new CSV file the column day and time. However, it is displaying an error when trying to write to the file ( writer.writerows(dia,hora)
); Error Message:
TypeError: writerows () takes exactly 2 arguments (3 given)
Follow the code below:
import csv
import re
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)
with open(os.path.join('teste2.csv'), 'wb') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames = ['dia','hora'], delimiter = ',')
writer.writeheader()
for row in reader:
data = row['created_at'].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()