-1

我正在尝试将 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

为什么呢?怎么了?

4

2 回答 2

5

int 和 str 是不同的类型。

要将 int 连接到 str,

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

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

编辑:您的插入语句在循环之外,试试这个:

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

于 2013-10-09T21:41:15.873 回答
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 + ";"
于 2013-10-09T22:48:09.183 回答