Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
writing a csv file
#1
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()
Reply
#2
Wjat is this line supposed to do?
list[x,y,z,p]
Python is case senditive
csvwriter.writerow(List)
Reply
#3
sorry it was list then also the same output
Reply
#4
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))
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Writing string to file results in one character per line RB76SFJPsJJDu3bMnwYM 4 1,370 Sep-27-2022, 01:38 PM
Last Post: buran
  Writing to json file ebolisa 1 998 Jul-17-2022, 04:51 PM
Last Post: deanhystad
  Writing to External File DaveG 9 2,486 Mar-30-2022, 06:25 AM
Last Post: bowlofred
  Writing to file ends incorrectly project_science 4 2,688 Jan-06-2021, 06:39 PM
Last Post: bowlofred
  Writing unit test results into a text file ateestructural 3 4,739 Nov-15-2020, 05:41 PM
Last Post: ateestructural
  Writing to file in a specific folder evapa8f 5 3,412 Nov-13-2020, 10:10 PM
Last Post: deanhystad
  Failure in writing binary text to file Gigux 7 3,782 Jul-04-2020, 08:41 AM
Last Post: Gigux
  writing data to a csv-file apollo 1 2,365 Jul-03-2020, 02:28 PM
Last Post: DeaD_EyE
  Writing to File Issue Flash_Stang 3 2,514 Jun-05-2020, 05:14 AM
Last Post: Gribouillis
  Help! Formatting and Writing to a File bwdu 2 2,410 Apr-19-2020, 09:29 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020