Python Forum
Problem while creating an Excel File using python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem while creating an Excel File using python
#1
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!
Reply
#2
see this SO answer https://stackoverflow.com/a/3348664/4046632
Reply
#3
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!
Reply
#4
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python openyxl not updating Excel file MrBean12 1 329 Mar-03-2024, 12:16 AM
Last Post: MrBean12
  Copy Paste excel files based on the first letters of the file name Viento 2 438 Feb-07-2024, 12:24 PM
Last Post: Viento
  Search Excel File with a list of values huzzug 4 1,242 Nov-03-2023, 05:35 PM
Last Post: huzzug
  Updating sharepoint excel file odd results cubangt 1 835 Nov-03-2023, 05:13 PM
Last Post: noisefloor
  Help creating shell scrip for python file marciokoko 10 1,363 Sep-16-2023, 09:46 PM
Last Post: snippsat
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,102 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Save and Close Excel File avd88 0 3,052 Feb-20-2023, 07:19 PM
Last Post: avd88
  Trying to access excel file on our sharepoint server but getting errors cubangt 0 815 Feb-16-2023, 08:11 PM
Last Post: cubangt
  Import XML file directly into Excel spreadsheet demdej 0 847 Jan-24-2023, 02:48 PM
Last Post: demdej
  how to read txt file, and write into excel with multiply sheet jacklee26 14 9,973 Jan-21-2023, 06:57 AM
Last Post: jacklee26

Forum Jump:

User Panel Messages

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