Python无法连接'str'和'int'对象

时间:2013-10-09 21:39:32

标签: python mysql xml parsing

我正在尝试将XML文件解析为mysql db。 这是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

为什么会这样?怎么了?

2 个答案:

答案 0 :(得分:5)

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执行一堆插入时。

答案 1 :(得分:0)

您的最终评论似乎是要求只向您的数据库添加一条记录....您需要在循环中构建一个更长的字符串

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 + ";"