-1

I am a beginner in python. How do I modify the code below to write out the list to a csv file with 2 columns of output as below.

1 1
2 1
3 1
4 1
5 1

Right now, the output is below

(1, 1)
(2, 1)
(3, 1)
(4, 1)
(5, 1)


list = [(1,1),(2,1),(3,1),(4,1),(5,1)]
with open(test.csv, 'wb') as testfile:
    csv_writer = csv.writer(testfile)
    for i in list:
        csv_writer.writerow([i])

Greatly appreciate your help.

L.Liu
  • 1
  • 1
  • 1
    Possible duplicate of [Writing List of Strings to Excel CSV File in Python](http://stackoverflow.com/questions/6916542/writing-list-of-strings-to-excel-csv-file-in-python) – Kredns Mar 09 '17 at 20:42
  • `csv_writer.writerow(i)` would get rid of the parens, but the comma would still be there... Is that close enough? – Kevin Mar 09 '17 at 20:44
  • csv_writer.writerow(str(i[0])+' '+str(i[1])) – plasmon360 Mar 09 '17 at 20:47
  • @user1753919 Thank you! This is exactly what I am looking for. – L.Liu Mar 09 '17 at 21:00
  • @Kevin your answer works, too. Thanks! – L.Liu Mar 09 '17 at 21:06
  • Possible duplicate of [How do I read and write CSV files with Python?](https://stackoverflow.com/q/41585078/608639), [Writing List of Strings to Excel CSV File in Python](https://stackoverflow.com/q/6916542/608639), [Write String and integer to CSV file](https://stackoverflow.com/q/44123543/608639), etc. – jww Mar 06 '19 at 12:08

4 Answers4

0

You are writing the tuple to the file and not each element of the tubple to the file. Try this below. Let me know if that gives you an error.

list = [(1,1),(2,1),(3,1),(4,1),(5,1)]
with open(test.csv, 'wb') as testfile:
    csv_writer = csv.writer(testfile)
    for i in list:
        csv_writer.writerow(i[0] + ' ' + i[1])
Brent
  • 51
  • 1
  • 8
  • using csv_writer.writerow(i[0] + ' ' + i[1]), I am getting this error. TypeError: can only concatenate tuple (not "str") to tuple – L.Liu Mar 09 '17 at 21:10
  • I think user1753919's solution works the best. I forgot you had to change the type to a string so str(i[0]) + ' ' + str(i[1]); does this give you an error? – Brent Mar 09 '17 at 21:43
  • Also, I think Apero's solution will work. You could change the %d to %s for a string instead of a decimal integer. – Brent Mar 09 '17 at 21:45
  • str(i[0]) + ' ' + str(i[1]) and csv_writer.writerow(i) both work. @Apero 's solution also works using either %d or %s probably because the data I am using. Thank you for the help! – L.Liu Mar 10 '17 at 15:24
0
import csv
list = [(1,1),(2,1),(3,1),(4,1),(5,1)]
with open('test.csv', 'wb') as testfile:
    csv_writer = csv.writer(testfile)
    for i in list:
        csv_writer.writerow(str(i[0])+' '+str(i[1]))
plasmon360
  • 4,109
  • 1
  • 16
  • 19
0

Your list is made of pairs, just iterate on it and pass the pair to a string template for formatting:

import csv
list = [(1,1),(2,1),(3,1),(4,1),(5,1)]
with open('test.csv', 'wb') as testfile:
    csv_writer = csv.writer(testfile)
    for pair in list:
        csv_writer.writerow('%d %d' % pair)
DevLounge
  • 8,313
  • 3
  • 31
  • 44
  • 1
    Whilst this code snippet is welcome, and may provide some help, it would be [greatly improved if it included an explanation](//meta.stackexchange.com/q/114762) of *how* and *why* this solves the problem. Remember that you are answering the question for readers in the future, not just the person asking now! Please [edit] your answer to add explanation, and give an indication of what limitations and assumptions apply. – Toby Speight Mar 10 '17 at 11:19
0

The csv.writer method takes an iterable. The i variable is already a tuple, so you don't need to make it a list. By enclosing i in [], you're turning it into a list of tuples: [(1, 2)]. Then csv.writer takes your list and splits it into a tuple of (1, 2) for the first item in the row.

Assuming you want to have a space for a delimiter instead of a comma, you'll need to do the following: 1) Add a delimiter parameter to the call to csv.writer and 2) get rid of the brackets.

Also, you might want to change the way you open the file to just 'w'. CSV files are supposed to be plain text, so you probably don't want to write it as binary.

Here's how I would change the code.

list = [(1,1),(2,1),(3,1),(4,1),(5,1)]
with open('test.csv', 'w') as testfile:
    csv_writer = csv.writer(testfile, delimiter=' ')
    for i in list:
        csv_writer.writerow(i)
Troy Hoffman
  • 181
  • 1
  • 5