简体   繁体   English

Python:将元组转换为逗号分隔的String

[英]Python: Convert tuple to comma separated String

import MySQLdb

db = MySQLdb.connect("localhost","root","password","database")
cursor = db.cursor()
cursor.execute("SELECT id FROM some_table")
u_data = cursor.fetchall()

>>> print u_data
((1320088L,),)

What I found on internet got me till here: 我在互联网上找到的东西让我直到这里:

string = ((1320088L,),)
string = ','.join(map(str, string))
>>> print string
(1320088L,)

what I expect output to look like: 我期望输出看起来像:

 #Single element expected result
 1320088L  
 #comma separated list if more than 2 elements, below is an example
 1320088L,1320089L

Use itertools.chain_fromiterable() to flatten your nested tuples first, then map() to string and join() . 使用itertools.chain_fromiterable()首先展平嵌套元组,然后map()为string和join() Note that str() removes the L suffix because the data is no longer of type long . 请注意, str()删除L后缀,因为数据不再是long类型。

>>> from itertools import chain
>>> s = ((1320088L,),)
>>> ','.join(map(str,chain.from_iterable(s)))
'1320088'

>>> s = ((1320088L,1232121L),(1320088L,),)
>>> ','.join(map(str,chain.from_iterable(s)))
'1320088,1232121,1320088'

Note, string is not a good variable name because it is the same as the string module. 注意, string不是一个好的变量名,因为它与string模块相同。

I think the string is a tuple of tuple containing long values. 我认为string是一个tupletuple含长值。

>>> string = ((1320088L,),)
>>> ','.join(str(y) for x in string for y in x if len(x) > 0)
'1320088'
>>>

eg with more than one value 例如,有多个值

>>> string = ((1320088L,1232121L),(1320088L,),)
>>> ','.join(str(y) for x in string for y in x if len(x) > 0)
'1320088,1232121,1320088'
>>>
string = ((1320088L,),)
print(','.join(map(str, list(sum(string, ())))))
string = ((1320088L, 1232121L), (1320088L,),)
print(','.join(map(str, list(sum(string, ())))))

Output: 输出:

1320088
1320088,1232121,1320088

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

相关问题 将浮点数列表转换为逗号分隔的字符串 | Python - Convert list of floats to comma separated string | Python 将字典列表转换为逗号分隔的字符串 python - Convert list of Dictionaries to comma separated string python 如何将逗号分隔的字符串转换为 Python 中的项目中包含逗号的列表? - How to convert comma separated string to list that contains comma in items in Python? 如何在 python 中将逗号分隔的字符串转换为逗号分隔的 int - how to convert comma separated string to comma seperated int in python 将python字典转换为逗号分隔的键字符串和逗号分隔值字符串的优雅方法是什么? - What is an elegant way to convert a python dictionary into a comma separated keys string and a comma separated values string? Python - 将逗号分隔的字符串转换为缩减字符串列表 - Python - convert comma separated string into reducing string list 如何在 python 的列表中转换带有逗号分隔值的字符串? - how to convert a string with comma separated values within a list in python? 将嵌套 JSON 中逗号分隔的字符串转换为列表 Python - Convert Comma Separated String in Nested JSON to List Python 将用逗号分隔的字符串数字转换为整数或在python中浮点数 - convert string numbers separated by comma to integers or floats in python Python 中字符串的频率(逗号分隔) - frequency of string (comma separated) in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM