简体   繁体   English

Python-将元组转换为字符串

[英]Python - convert Tuple into String

I have a connection to my MySQL DB. 我已经连接到我的MySQL数据库。 And my program reads the Selected Objects and makes a tuple out of it (result) 我的程序读取“选定对象”并从中生成一个元组(结果)

a = """SELECT plates FROM plat"""
cursorO.execute(a)
connectionoracle.commit()
result = cursorO.fetchall()
print result

Now, I want to write the content of "result" into an other db. 现在,我想将“结果”的内容写入另一个数据库。 I try this: 我尝试这样:

s = result

cursorM.execute("""UPDATE `anpr_localhost`.`plat` SET
`Plat` = %s
WHERE `ID` = 1;""", (s))
connectionmysql.commit()

cursorM.close()

This is simply not working, because result is a tuple and not a string. 这根本行不通,因为结果是元组而不是字符串。 I already tried 我已经试过了

result.join(map(str, result), '.')

And different variations of this, but my Consule always tells me that tuple object has no function "join" and things like that. 以及它的不同变体,但是我的领事总是告诉我,元组对象没有函数“ join”之类的东西。

How can I convert my tuple (result) to a normal string ? 如何将元组(结果)转换为普通字符串?

Just join after you have mapped ".".join(map(str, result)) 映射".".join(map(str, result))后只需加入即可".".join(map(str, result))

result =(1,2,3,4,)

print ".".join(map(str, result))

1.2.3.4

Or if a list of tuples: 或者,如果有一个元组列表:

" ".join(".".join(map(str, s)) for s in result)

If the result is a list of tuples, you can try to do the following: 如果结果是元组列表,则可以尝试执行以下操作:

results = [".".join(map(str,r)) for r in result]
print ".".join(results)

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

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