我有这个python程序,它将字符串添加到整数:
a = raw_input("Enter a: ")
b = raw_input("Enter b: ")
print "a + b as strings: " + a + b
a = int(a)
b = int(b)
c = a + b
str(c)
print "a + b as integers: " + c
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
Python: TypeError: cannot concatenate 'str' and 'int' objects
Run Code Online (Sandbox Code Playgroud)
如何将字符串添加到整数?
在我遇到问题之前,我一直在寻找答案,但似乎无法找到我的案例.
好吧,基本上我通过cmd调用我的脚本并传入16个args并使用它们来设置我拥有的一些变量.我正在为公司使用创建自定义HTML报告.这些变量我只是用来动态设置我想要的值在html字符串中.我得到的错误是:
>>> python -u "htmltest.py" 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Traceback (most recent call last):
File "htmltest.py", line 162, in <module>
<TD STYLE="border-top: 1px solid #000000; border-bottom: 1px solid #000000; border- left: 1px solid #000000; border-right: 1px solid #000000" COLSPAN=3 ALIGN=LEFT VALIGN=BOTTOM SDNUM="1033;0;0.000000"><FONT FACE="Calibri" COLOR="#000000">"""+C9+"""</FONT></TD>
TypeError: cannot concatenate 'str' and 'type' objects
>>> Exit Code: 1
Run Code Online (Sandbox Code Playgroud)
我已经尝试删除一些变量,如C9等,看看它做了什么,但它只是前一个的错误,所以我假设我必须使变量与我如何做我的字符串相同?
代码是:
import sys
import datetime
#for each arg sent we can …Run Code Online (Sandbox Code Playgroud) 我现在正在学习Python,耶!无论如何,我有小问题.我在这里看不到问题:
x = 3
y = 7
z = 2
print "I told to the Python, that the first variable is %d!" % x
print "Anyway, 2nd and 3rd variables sum is %d. :)" % y + z
Run Code Online (Sandbox Code Playgroud)
但Python认为不同 - TypeError: cannot concatenate 'str' and 'int' objects.
为什么会这样?我没有像字符串那样设置任何变量......就像我看到的那样.
编写python程序,我在使用该urllib.urlopen函数时想出了这个错误.
Traceback (most recent call last):
File "ChurchScraper.py", line 58, in <module>
html = GetAllChurchPages()
File "ChurchScraper.py", line 48, in GetAllChurchPages
CPs = CPs + urllib.urlopen(url)
TypeError: cannot concatenate 'str' and 'instance' objects
url = 'http://website.com/index.php?cID=' + str(cID)
CPs = CPs + urllib.urlopen(url)
Run Code Online (Sandbox Code Playgroud) 我得到一个TypeError:无法连接'str'和'list'对象.
我试图从列表中传递一个对象,通过将它与另一个变量连接来创建一个新变量.
示例:我想从组列表中获取值并将其与"All.dbf"连接,以便它对列表中的每个值使用该文件执行某些操作.如果正常工作,它会在每次运行时将dbname的值分别设置为AdministrativeAll.dbf,CadastralAll.dbf和PlanimetericAll.dbf,但是我得到'str'和'list'错误....
group = ['Administrative', 'Cadastral', 'Planimetric']
for i in group:
dbname = i + "All.dbf"
blah, blah, blah....
Run Code Online (Sandbox Code Playgroud)
我想我可以将"All.dbf"添加到组列表中的值,但认为必须有更好的方法来处理这个函数或我不知道的事情....任何想法?
干杯