Nucklear
Nucklear

Reputation: 478

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.

Upvotes: 1

Views: 236

Answers (3)

Marcel
Marcel

Reputation: 3278

The first line of your CSV should be:

sep=,

This manner your are saying to Excel to delimit the content by "," automatically.

Upvotes: 0

Susam Pal
Susam Pal

Reputation: 34294

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

Upvotes: 2

Christian Witts
Christian Witts

Reputation: 11585

For Excel to automatically parse your .CSV and split it into columns without you either do so or it asking, your delimiter will need to match your Locale Delimiter which you can find under

Control Panel -> Region & Language -> Additional Settings -> List Seperator

And that needs to match the column delimiter you are using in your .CSV file for Excel to open and split the columns correctly.

Upvotes: 2

Related Questions