Python Forum
Problem while creating an Excel File using python - 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: Problem while creating an Excel File using python (/thread-6817.html)



Problem while creating an Excel File using python - Math_Enthusiast - Dec-09-2017

Hi,

I have written a small python code for creating an excel file with 2 columns.But,after running the code, My data in the CSV File appears to be like this:

nodeLabel VonmissesStress

1 650

1 550

My code is as following:

with open('C:\\Users\\harsha\\SetData.csv', 'w') as csvfile:
fieldnames = ['nodeLabel', 'VonmissesStress']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
writer.writerow({'nodeLabel': '1', 'VonmissesStress': 'StressData'})
writer.writerow({'nodeLabel': '1', 'VonmissesStress': 'StressData'})


Could someone explain,Why I am getting one extra empty row between two filled rows.I would be really glad,if you can modify my code,so that there are no extra rows in between two filled rows.
Thanks in Advance!


RE: Problem while creating an Excel File using python - buran - Dec-09-2017

see this SO answer https://stackoverflow.com/a/3348664/4046632


RE: Problem while creating an Excel File using python - Math_Enthusiast - Dec-09-2017

Hi Buran,

Thanks a lot for your reply.
I tried the one,described in the link,you have posted. But that does nt seem to work in python 3.5.3 version.

I tried out differently:

with open('C:\\Users\\harsha\\SetData.csv', 'w') as csvfile:
fieldnames = ['nodeLabel', 'VonmissesStress']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames, lineterminator='\n')
writer.writeheader()
writer.writerow({'nodeLabel': '1', 'VonmissesStress': 'StressData'})
writer.writerow({'nodeLabel': '1', 'VonmissesStress': 'StressData'})

This is working fine.Thanks a lot!


RE: Problem while creating an Excel File using python - buran - Dec-09-2017

(Dec-09-2017, 08:36 PM)Math_Enthusiast Wrote: But that does nt seem to work in python 3.5.3 version.
well, obviously you are doing something wrong - it's official docs:
https://docs.python.org/3.5/library/csv.html#csv.writer
see the example