0

I am trying to write some values onto a CSV file but all the values are getting jammed into one column how do I use a different column for each value? Please go easy on me I am an absolute beginner with programming. It would be really helpful if someone could write an example code on how to solve this issue

Here is my code:

with open('log.csv', 'a',) as file:
    fieldnames = ['Timestamp', 'Overall result', 'Soll-orderno', 'Desired-HW-Version', 'Desired-SF-Version',
     'Desired-productcode', 'Desired-device-type', 'Scancode', 'Wbm-orderno', 'Wbm-HW-Version', 'Wbm-SF-Version',
      'Wbm-mac-address', 'test-product-code', 'combined-product-code' , 'wbm-device-type', 'test-device-type']
    writer = csv.DictWriter(file, fieldnames=fieldnames)

    writer.writeheader()
    writer.writerow({'Timestamp':now, 'Overall result':'Blank', 'Soll-orderno':d_ordernum, 'Desired-HW-Version':d_hw_version, 'Desired-SF-Version':d_sf_version,
     'Desired-productcode':pc_praefix, 'Desired-device-type':d_dev_typ, 'Scancode':scancode_string, 'Wbm-orderno':ord_nmr, 'Wbm-HW-Version':v, 'Wbm-SF-Version':b,
      'Wbm-mac-address':mac_addr, 'test-product-code':'Blank', 'combined-product-code':product_code, 'wbm-device-type':dev_typ, 'test-device-type':'Blank'})

when I open the csv file using Excel it SHOULD look like this: What the excel sheet is supposed to look like

But unfortunately my CSV file looks like this when i open it with Excel What the excel sheet looks like now

As you can see all the values have gotten jam packed into one column instead of 1value:1column I would really appreciate it if you guys could show me how to do this. I think i am doing everything correctly but it still gets written only to one column. Please show me a solution for this problem if you guys can.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 2
    When you open it in Excel you need to tell Excel to use commas as the column separators. For whatever reason, it assumes you're going to want to use tabs. – Kemp Apr 19 '21 at 13:37
  • @Kemp tab is the default delimiter for .txt files, comma is the default delimiter for .csv files (hence the name, csv = comma separated values). – Nicholas Hunter Apr 19 '21 at 13:42
  • @NicholasHunter I know that, but historically I've had issues with Excel assuming tabs when opening a csv file and that seemed to match what was happening in the question. I've checked just now with Excel 365 and it doesn't seem to happen though. – Kemp Apr 19 '21 at 14:27
  • A separate issue that I can see happening in that screenshot is the extra blank lines you get when using the csv module on Windows. See [here](https://stackoverflow.com/questions/3348460/csv-file-written-with-python-has-blank-lines-between-each-row) for the solution. – Kemp Apr 19 '21 at 14:28
  • You’re appending to the output file; what’s already in the file may be making excel think the separator is tab. And you should open the file with `newline=‘’` as shown in _all_ the examples in the `csv` documentation https://docs.python.org/3/library/csv.html?highlight=csv#module-csv – DisappointedByUnaccountableMod Jul 24 '21 at 10:15

0 Answers0