6
I need my script write to a CSV file. It 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 day and time column. However, you are experiencing error while trying to write to the file (writer.writerows(dia,hora)
); displays error message :
Typeerror: writerows() takes Exactly 2 Arguments (3 Given)
Follows code:
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()
After change to: Writer.writerows([dia,hora]) displays the following message : Traceback (Most recent call last): File "C: Test.py", line 25, in <module> Writer.writerows([dia,hora]) File "C: Python27 lib csv.py", line 157, in writerows Rows.append(self. _dict_to_list(rowdict)) File "C: Python27 lib csv.py", line 148, in _dict_to_list + ", ". Join([Repr(x) for x in wrong_fields]) Valueerror: Dict contains Fields not in fieldnames: '1', '9'
– Marina Oliveira
Edit your question and add a fragment (the first 10 lines, for example) of the csv file you have to read, and the last error message that is occurring.
– Sidon