Python不能连接'str'和'int'对象

2024-06-10 20:49:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试将XMLfile解析为mysqldb。 这是xml

<categories>
<category id="28">Woman</category>
<category id="277" parentId="28">T-Shirts</category>
<category id="140" parentId="277">shorts</category>
</category>

.py年

for category in categories:
        for item in category.getElementsByTagName("category"):
            category_name = item.childNodes[0].nodeValue.encode("utf-8")
            category_id = int(item.getAttribute('id'))
            category_parentId = item.getAttribute('parentId')
#connect etc
sqlFillCategories = "INSERT INTO categories(category_id, category_parentId, shop_id, category_name) VALUES ('"+category_id + "', '" + category_parentId + "', '" + TREND_BRANDS_SHOPID + "', '" + category_name + "');"

错误:

Traceback (most recent call last):
  File "E:/python1/fanswell/parse.py", line 23, in <module>
    sqlFillCategories = "INSERT INTO categories(category_id, category_parentId, shop_id, category_name) VALUES ('"+category_id + "', '" + category_parentId + "', '" + TREND_BRANDS_SHOPID + "', '" + category_name + "');"

TypeError: cannot concatenate 'str' and 'int' objects

为什么会这样?怎么了?


Tags: nameinpyidforshopitemint
2条回答

int和str是不同的类型。

要将int连接到str

您需要进行intstr的转换。 也就是说,例如:

"Hello World " + str(1)

所以你可能想:

 sqlFillCategories = "INSERT INTO categories(category_id, category_parentId,
 shop_id, category_name) VALUES ('"+str(category_id) + "', '" +
 str(category_parentId) + "', '" + str(TREND_BRANDS_SHOPID) + "', '" 
 + category_name + "');"

编辑: insert语句超出循环范围,请尝试以下操作:

sqlFillCategories =''
for category in categories:
        for item in category.getElementsByTagName("category"):
            category_name = item.childNodes[0].nodeValue.encode("utf-8")
            category_id = int(item.getAttribute('id'))
            category_parentId = item.getAttribute('parentId') 
            sqlFillCategories += 
            "INSERT INTO categories(category_id, category_parentId, shop_id, 
            category_name) VALUES ('"+category_id + "', '" + 
            category_parentId + "','" + TREND_BRANDS_SHOPID + "',
             '" + category_name + "');"

sqlFillCategories它将执行一组插入。

你的最后一条评论似乎是想问你的数据库只添加一条记录。。。。您需要在循环中构建一个较长的字符串

VALUES ('category', 'catid', 'catParentId', store, 'category'), 
('category', 'catid', 'catParentId', store, 'category'), 
('category', 'catid', 'catParentId', store, 'category')



allValues = []
for category in categories:
    for item in category.getElementsByTagName("category"):
        category_name = item.childNodes[0].nodeValue.encode("utf-8")
        category_id = int(item.getAttribute('id'))
        category_parentId = item.getAttribute('parentId')
        myValue = "('"+str(category_id) + "', '" + str(category_parentId) + "', 
        '" + str(TREND_BRANDS_SHOPID) + "', '" + category + "')"
        allValues.append(myValue)

comma_separated = ', '.join(allValues)
sqlFillCategories = "INSERT INTO categories(category_id, category_parentId,
 shop_id,
 category_name) VALUES " + comma_separated + ";"

相关问题 更多 >