Reputation: 109
I am supposed to generate a csv file with 10 columns (from A to J) with 10000 rows that contain random numbers between 1 to 1000. I am able to get the desired output but cannot write this output to a csv file. Please find my code and the generated error message below.
import random
import csv
with open('table.csv', 'w') as file:
csv_write = csv.writer(file,delimiter="\t")
for i in range(ord('A'), ord('J')+1):
csv_write.writerow(chr(i), end="\t")
for j in range(1,11):
for k in range(1,1001):
csv_write.writerow(random.randint(1,10001), end="\t")
Error Message
Traceback (most recent call last):
File "C:\Users\rida_\Desktop\tables.py", line 8, in <module>
csv_write.writerow(chr(i), end="\t")
TypeError: writerow() takes no keyword arguments
Upvotes: 0
Views: 1628
Reputation: 11073
According to this document when you open a CSV file for write, you will give a delimiter
and quotechar
and other staff, so here in your case you dont need to pass end="\t"
to writerow
. this method just gives a list to write in your file. good luck.
Upvotes: 0
Reputation: 2270
It looks like writenow only takes the string as an arg, nothing else.
https://docs.python.org/2/library/csv.html#csv.csvwriter.writerow
If you want a \t
at the end just do chr(i)+'\t'
. Char just converts a single character though. I think you're looking for str(i)+'\t'
So,
csv_write.writerow(str(i)+'\t')
And
csv_write.writerow(str(random.randint(1,10001))+'\t'
When you specified delimiter \t
when creating the csv_write I think it will end the row with \t
anyway so you don't need to append it to the end. Might have to try it out.
Upvotes: 2