Python、XML 和 MySQL - ascii v utf8 编码问题

标签 python mysql xml encoding utf-8

我有一个 MySQL 表,XML 内容存储在一个长文本字段中,编码为 utf8mb4_general_ci

数据库表 enter image description here 我想使用 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/etre‌​e/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/

相关文章:

mysql - 当 table_b 中的 certain_field 为 null 时,在 table_a 左连接 table_b 中选择 *

python - Mapper Mapper|用户|用户无法组装映射表的任何主键列 'users'

python - 无法从 Python 进程中返回 append 值

python - Pandas 子集并根据列值删除行

c# - MySql C# - 使用 PhPmyAdmin 访问,但不能使用应用程序访问

java - 使用 Jackson XML 映射器将 Java 列表序列化为 XML

python - pytest 标记命令不起作用

mysql - 如何跟踪更新的行数

Android XML 调整大小/适合 ImageButton 不同的屏幕

java - Android - ViewFlipper 问题