我正在尝试将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
为什么会这样?怎么了?
int和str是不同的类型。
要将int连接到str
您需要进行
int
到str
的转换。 也就是说,例如:所以你可能想:
编辑: insert语句超出循环范围,请尝试以下操作:
当
sqlFillCategories
它将执行一组插入。你的最后一条评论似乎是想问你的数据库只添加一条记录。。。。您需要在循环中构建一个较长的字符串
相关问题 更多 >
编程相关推荐