简体   繁体   中英

convert a tuple into a string in python

i have a tuple

tup = ('\x00\x05(^\x9a\xdd\x1c\xb3\xe0T\x00!(\xa8z\xd8', 0, 'ABC', 0, None, None, None, None, None, None, None)

i want to convert it in a pipe separated string

\x00\x05(^\x9a\xdd\x1c\xb3\xe0T\x00!(\xa8z\xd8|0|ABC|0|None|None|None|None|None|None

I'm doing something like this and getting following errors

''.join(tup)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: sequence item 1: expected string, int found

You need to convert each element to a string first. You can do this with map and str :

print '|'.join(map(str, tup))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM