2

I need help with writing values to a csv file.

I have 4 lists of values that I would like to write to a csv file, but not in a normal way. I mean, usually the csv module write the values in the same row, but this time I would like to write the values of the lists in different columns, I mean one column and different rows for every list. In this way, all the list 1 data would be in the column A of Excel, all the list 2 data would be in the column B of excel, and so on. Now I was trying a lot of commands and I half did it, but not at all.

My list's names are: It_5minute, Iiso_5min, IHDKR_5min and Iperez_5min.

My actual commands:

with open('Test.csv', 'w') as f:
    w = csv.writer(f)
    for row in zip(It_5minute, Iiso_5min, IHDKR_5min,Iperez_5min):
        w.writerow(row)

With these commands I get the list values in the same column (instead of every list in a different column), each value separated by comma. I have attached an Excel image to clarify the problem. I want each list in a separated column, to be able of do operations with the data in an easy way. Can anybody help me? Thank you very much.

PD: Would be nice to write the name of each list at the top of every column, too.

enter image description here

3
  • 2
    I believe it has nothing to do with python or csv but with the excel. Commented Nov 10, 2013 at 18:23
  • You could use a different delimiter, like a space or tabs, and see if Excel plays nicely with the import. Commented Nov 10, 2013 at 18:29
  • I tried with a space between the values but was I still got the same. All the values in the same column. Commented Nov 10, 2013 at 18:32

1 Answer 1

1

Just for the fact change with open('Test.csv', 'w') as f: to with open('Test.csv', 'wb') as f: as csv's are binary.

State the delimiter to use clearly (in this case a comma) and whether or not to use quoting just in case (optional)

with open('Test.csv', 'wb') as f:
    w = csv.writer(f,delimiter=',', quoting = csv.QUOTE_ALL) #you can replace the delimiter for whatever that suits you
    for row in zip(It_5minute, Iiso_5min, IHDKR_5min,Iperez_5min):
        w.writerow(row)

In case this doesn't work, you have to manually state the delimiter in the excel text import wizard. You can read how to here

Common Delimiters:

Tab = '\t'

semicolon = ';'

comma = ','

space = ' '

eg: comma selected as the delimiter

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

I don't know why but Python doesn't accept 'with open('Test.csv', 'wb') as f:', I receive the following error: TypeError: 'str' does not support the buffer interface... Doing it manually in Excel as you said works perfectly. Thank you very much for answering!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.