gpt4 book ai didi

python:无法连接 'str' 和 'tuple' 对象(应该可以!)

转载 作者:太空狗 更新时间:2023-10-29 19:34:57 25 4
gpt4 key购买 nike

我有一个代码:

print "bug " + data[str.find(data,'%')+2:-1]
temp = data[str.find(data,'%')+2:-1]
time.sleep(1)
print "bug tuple " + tuple(temp.split(', '))

然后我的应用程序显示:

bug 1, 2, 3 Traceback (most recent call last): File "C:\Python26\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 312, in RunScript exec codeObject in main.dict File "C:\Documents and Settings\k.pawlowski\Desktop\atsserver.py", line 165, in print "bug tuple " + tuple(temp.split(', ')) TypeError: cannot concatenate 'str' and 'tuple' objects

我不知道我做错了什么。 print tuple('1, 2, 3'.split(', ')) 工作正常。

最佳答案

print tuple(something)

可能会起作用,因为 print 会对参数执行隐式 str(),但是表达式如下

"" + ()

不起作用。您可以单独打印它们这一事实并没有什么区别,您不能连接字符串和元组,您必须转换其中任何一个。即

print "foo" + str(tuple("bar"))

但是,依赖于 str() 进行转换可能不会给出预期的结果。使用“,”分隔符整齐地连接它们。例如连接

关于python:无法连接 'str' 和 'tuple' 对象(应该可以!),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3609637/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com