-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.

6
  • 1
    Possible duplicate of Writing List of Strings to Excel CSV File in Python Commented Mar 9, 2017 at 20:42
  • csv_writer.writerow(i) would get rid of the parens, but the comma would still be there... Is that close enough? Commented Mar 9, 2017 at 20:44
  • csv_writer.writerow(str(i[0])+' '+str(i[1])) Commented Mar 9, 2017 at 20:47
  • @user1753919 Thank you! This is exactly what I am looking for. Commented Mar 9, 2017 at 21:00
  • @Kevin your answer works, too. Thanks! Commented Mar 9, 2017 at 21:06

4 Answers 4

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]))
Sign up to request clarification or add additional context in comments.

Comments

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])

4 Comments

using csv_writer.writerow(i[0] + ' ' + i[1]), I am getting this error. TypeError: can only concatenate tuple (not "str") to tuple
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?
Also, I think Apero's solution will work. You could change the %d to %s for a string instead of a decimal integer.
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!
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)

1 Comment

Thank you so much for detailed explanations!
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)

1 Comment

Whilst this code snippet is welcome, and may provide some help, it would be greatly improved if it included an explanation 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.

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.