1

I have a requirement, where i need to generate a number with year and month(concatenated), and write the same numbers into csv, which should be in row by row, tried below code,

import csv

l = []
for x in range(2019,2031):
    for i in range(1,13):
        m = str(x)+str(i)
        l.append(m)
        #l.append('\n')
    writer = csv.writer(open("path\\test.csv","w"))
    writer.writerows([l])

But i'm getting values in single row, not row by row(in single column)

1 Answer 1

3

You need to use writerow().

import csv

l = []
for x in range(2019,2031):
    for i in range(1,13):
        m = str(x)+str(i)
        l.append(m)
writer = csv.writer(open("path\\test.csv","w",newline=''))
for each in l:
    writer.writerow([each])
Sign up to request clarification or add additional context in comments.

2 Comments

Tried your code, got values row by row, however, after every row i'm getting empty row. 20191 20192 20193 20194 20195
just add newline='' to open. I am editing the code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.