简体   繁体   English

在python中将元组转换为字符串

[英]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 : 您可以使用mapstr进行此操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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