我正在编写一个 python 脚本来创建一个基于 MySql 数据库的 mongo 集合。问题在于微标志字符:
bson.errors.InvalidStringData: strings in documents must be valid UTF-8: '\xb5g'
我尝试使用不同的代码(utf-8、latin-1、cp1252、iso-8859-2)对值进行编码/解码但没有成功,但我总是收到以下错误:
UnicodeDecodeError: 'ascii' codec can't decode byte 0xb5 in position 0: ordinal not in range(128)
这是从 mysql 数据库中获取数据的代码。数据库是 USDA 一个 0:
# -*- encoding: utf-8 -*-
import MySQLdb
mysqldb = MySQLdb.connect(DBCONF)
cursor = mysqldb.cursor()
foodid = 1001
q = (
' SELECT nut.Nutr_Val,'
' nutdef.Units,'
' nutdef.NutrDesc, nutdef.Tagname'
' FROM food_des AS f'
' JOIN nutrient AS nut ON nut.NDB_No = f.NDB_No'
' JOIN nutrient_def AS nutdef ON nutdef.Nutr_No = nut.Nutr_No'
' WHERE f.NDB_No = %s'
) % str(foodid)
self.cursor.execute(q)
带有微符号字符的字段是 nutdef.Units 之一。