Why can't I join this tuple in Python?-ThrowExceptions

Exception or error:
e = ('ham', 5, 1, 'bird')

I have to join it so that I can write it into a text file.

How to solve:

join only takes lists of strings, so convert them first

>>> e = ('ham', 5, 1, 'bird')
>>> ','.join(map(str,e))

Or maybe more pythonic

>>> ','.join(str(i) for i in e)


join() only works with strings, not with integers. Use ','.join(str(i) for i in e).


You might be better off simply converting the tuple to a list first:

e = ('ham', 5, 1, 'bird')
liste = list(e)


Use the csv module. It will save a follow-up question about how to handle items containing a comma, followed by another about handling items containing the character that you used to quote/escape the commas.

import csv
e = ('ham', 5, 1, 'bird')
with open('out.csv', 'wb') as f:

Check it:

print open('out.csv').read()



Leave a Reply

Your email address will not be published. Required fields are marked *