Nucklear Nucklear - 2 months ago 11
Python Question

CSV file creation issue

I'm creating a csv file from a python script with this lines:

def createFile():
logfile = csv.writer(open('logfile.csv', 'wb'), dialect='excel')
formater = zip(dates, hours, threadids, loglevels, errorcodes, errormessage)
for i in formater:
logfile.writerow(i)


And everything works fine until I open the file with Excel because it opens the whole text in one column. This is how one row from the csv looks like:

4/29/12,22:44:32:865 EDT,0000004b,A,CHFW0019I,The Transport Channel Service has started chain chain_308.


Is there any way to open the file splitted in columns how it should be?
Regards.

Answer

I saved the following in foo.csv and it opens fine in Excel.

4/29/12,22:44:32:865 EDT,0000004b,A,CHFW0019I,The Transport Channel Service has started chain chain_308.
4/29/12,22:44:32:865 EDT,0000004b,A,CHFW0019I,The Transport Channel Service has started chain chain_308.
4/29/12,22:44:32:865 EDT,0000004b,A,CHFW0019I,The Transport Channel Service has started chain chain_308.
4/29/12,22:44:32:865 EDT,0000004b,A,CHFW0019I,The Transport Channel Service has started chain chain_308.
4/29/12,22:44:32:865 EDT,0000004b,A,CHFW0019I,The Transport Channel Service has started chain chain_308.

enter image description here

You might want to check if you have the right list separator configured in your regional settings using one of the following:

  1. Windows Start Button > Control Panel > Regional and Language Options > Regional Options > Customize > List Separator.
  2. Windows Start Button > Control Panel > Regional and Language Options > Formats > Additional Settings > List Separator.

Note that you can also launch 'Regional and Language Options' with the command intl.cpl, i.e. press Windows + R button and enter intl.cpl and press the 'OK' button.

enter image description here