47

I have a list of tuples ordered by value. They are in the form (name,count) where count is number of occurrences for each unique name.

I would like to take this list and transform it into CSV where each name is column header and each value is column value of a single row.

Any suggestions how to do it? Thanks.

mechanical_meat
  • 163,903
  • 24
  • 228
  • 223
Edmon
  • 4,752
  • 4
  • 32
  • 42
  • Please edit your post to include your attempt at a solution. Folks will point you in the right direction. Hint: have a gander at the `csv` module in the standard-library. – mechanical_meat Mar 22 '13 at 19:26
  • 2
    Just write a string `output += myTuple[0] + ', ' + myTuple[1] + '\n'` in a for loop. When the loop is done write the `output` to a `*.csv` file. –  Mar 22 '13 at 19:27
  • just `import csv` and read the module docs, very simple. http://docs.python.org/2/library/csv.html – MGP Mar 22 '13 at 19:30

2 Answers2

105

You can do this:

import csv

# note: If you use 'b' for the mode, you will get a TypeError
# under Python3. You can just use 'w' for Python 3

data=[('smith, bob',2),('carol',3),('ted',4),('alice',5)]

with open('ur file.csv','wb') as out:
    csv_out=csv.writer(out)
    csv_out.writerow(['name','num'])
    for row in data:
        csv_out.writerow(row)

    # You can also do csv_out.writerows(data) instead of the for loop

the output file will have:

name,num
"smith, bob",2
carol,3
ted,4
alice,5
dawg
  • 98,345
  • 23
  • 131
  • 206
  • Quite irregularly, writerow() puts quotation marks around SOME of the strings only. My raw data does not have quotation marks at all. Anything on that? – imrek Mar 09 '16 at 12:04
  • If you're on Windows, open the file with mode 'wb' to avoid getting a blank line between every row in the output. – rleelr Oct 06 '16 at 14:26
  • 42
    In Python 3, use mode "w" instead of "wb", to avoid getting a TypeError. You can pass `newline=''`, which will take care of the redundant blank lines. – Tomty Nov 20 '17 at 02:49
  • 2
    I'm getting this error, byte like object is required not str. – Abdul Haseeb Oct 31 '20 at 10:12
  • 1
    @AbdulHaseeb: That is from opening the file in binary mode `open('ur file.csv','wb')` The `b` in the mode with Python 3. Just remove the `b` in the open mode. – dawg Oct 31 '20 at 11:14
  • ok but how do we get rid of that empty rows written? Seems open with 'w' you get extra newline every line – user3761555 Mar 31 '23 at 02:44
5

Python, transposing a list and writing to a CSV file

import csv   
lol = [(1,2,3),(4,5,6),(7,8,9)]
item_length = len(lol[0])

with open('test.csv', 'wb') as test_file:
  file_writer = csv.writer(test_file)
  for i in range(item_length):
    file_writer.writerow([x[i] for x in lol])

output

1,4,7
2,5,8
3,6,9

Note that trying it in python 3 might give the error as mentioned in TypeError: a bytes-like object is required, not 'str' in python and CSV.

Consider to use with open('ur file.csv','w') as out: for python 3.

0x90
  • 39,472
  • 36
  • 165
  • 245