Python Forum

Full Version: Problem while creating an Excel File using python
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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!
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!
(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