问题描述:在Pyhton中使用拼接形式去组合产生新的字符串时,报can only concatenate str (not "int") to str
index =1
xml_data.xpath('//*[@id="id_' + index + '"]/@value')[0]
解决方法:出现该问题是由于Python中的字符串拼接和Java中是不一样,字符串拼接int类型时,不会做自动转换,因此这个时候需要使用 str() 进行强制转换
index =1
xml_data.xpath('//*[@id="id_' + str(index) + '"]/@value')[0]
当尝试在Python中将字符串与整数拼接时,遇到错误"can only concatenate str (not 'int') to str"。解决这个问题的关键在于,Python不自动将整数转换为字符串进行拼接,需要手动使用`str()`函数将整数转换为字符串。
4456

被折叠的 条评论
为什么被折叠?



