我有一个 MySQL 表,XML 内容存储在一个长文本字段中,编码为 utf8mb4_general_ci
数据库表 我想使用 Python 脚本从 transcript 字段中读取 XML 数据,修改一个元素,然后将值写回数据库。
当我尝试使用 ElementTree.tostring
将 XML 内容放入元素中时,出现以下编码错误:
Traceback (most recent call last):
File "ImageProcessing.py", line 33,
in <module> root = etree.fromstring(row[1])
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/etree/ElementTree.py", line 1300,
in XML parser.feed(text)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/etre e/ElementTree.py", line 1640,
in feed self._parser.Parse(data, 0)
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2014' in position 9568: ordinal not in range(128)
代码:
import datetime
import mysql.connector
import xml.etree.ElementTree as etree
# Creates the config parameters, connects
# to the database and creates a cursor
config = {
'user': 'username',
'password': 'password',
'host': '127.0.0.1',
'database': 'dbname',
'raise_on_warnings': True,
'use_unicode': True,
'charset': 'utf8',
}
cnx = mysql.connector.connect(**config)
cursor = cnx.cursor()
# Structures the SQL query
query = ("SELECT * FROM transcription")
# Executes the query and fetches the first row
cursor.execute(query)
row = cursor.fetchone()
while row is not None:
print(row[0])
#Some of the things I have tried to resolve the encoding issue
#parser = etree.XMLParser(encoding="utf-8")
#root = etree.fromstring(row[1], parser=parser)
#row[1].encode('ascii', 'ignore')
#Line where the encoding error is being thrown
root = etree.fromstring(row[1])
for img in root.iter('img'):
refno = img.text
img.attrib['href']='http://www.link.com/images.jsp?doc=' + refno
print img.tag, img.attrib, img.text
row = cursor.fetchone()
cursor.close()
cnx.close()
最佳答案
您已设置好所有内容,并且您的数据库连接正在返回 Unicode,这是一件好事。
不幸的是,ElementTree 的 fromstring()
需要字节 str 而不是 Unicode。这样 ElementTree 就可以使用 XML header 中定义的编码对其进行解码。
您需要改用它:
utf_8_xml = row[1].encode("utf-8")
root = etree.fromstring(utf_8_xml)
关于Python、XML 和 MySQL - ascii v utf8 编码问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36155659/