0

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
1
  • Did you think writerow() should take keywords for some reason? You indentation is off by the way, please fix it so that it is more readable for us. Commented Apr 17, 2018 at 3:39

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.