Python Forum
writing a csv file - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: writing a csv file (/thread-23193.html)



writing a csv file - vignesh2002 - Dec-16-2019

when a write multiple rows in csv file and open it in excel each row is appended leaving 1 row blank between them.
for ex-
when i write 5 rows, it gets appended in rows 1,3,5,7,9
Can anyone tell me why it is happening?
def a():
    x=input('Enter book ID')
    y=input('Enter book name')
    z=input('Enter author\'s name')
    p=input('Enter `book category')
    list[x,y,z,p]
    with open('books.csv', 'a') as csvfile:  
        csvwriter = csv.writer(csvfile)              
        csvwriter.writerow(List)  
for i in range(0,10):
    a()



RE: writing a csv file - woooee - Dec-16-2019

Wjat is this line supposed to do?
list[x,y,z,p]
Python is case senditive
csvwriter.writerow(List)



RE: writing a csv file - vignesh2002 - Dec-16-2019

sorry it was list then also the same output


RE: writing a csv file - Axel_Erfurt - Dec-16-2019

you do not need csvwriter for that.

create a list (mlist) and save it on the end.

example for a tab delimited csv

mlist = []
header = f'ID\tName\tAuthor\tCategory'
mlist.append(str(header))

def a():
    x=input('Enter book ID: ')
    y=input('Enter book name: ')
    z=input('Enter author\'s name: ')
    p=input('Enter book category: ')
    row = f"{x}\t{y}\t{z}\t{p}"
    mlist.append(str(row))
    
for i in range(0, 10):
    a()
    
with open('books.csv', 'w') as csvfile:               
    csvfile.write('\n'.join(mlist))