У меня есть таблицы MySQL с содержанием XML, хранящимся в LONGTEXT поля, закодированном в utf8mb4_general_ciPython, XML и MySQL - ASCII v utf8 кодирование вопросы
Таблица базы данных Я хочу использовать сценарий Python для чтения в XML-данных из поля транскрипта, измените элемент, а затем запишите значение обратно в базу данных.
Когда я пытаюсь получить содержимое XML в элемент с помощью ElementTree.tostring
я получаю следующее сообщение об ошибке кодирования:
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()
Просьба предоставить полную трассировку стека ошибок –
Симус, мы в опасности увеличения объема оригинального вопроса. Вы должны создать новый вопрос для новой проблемы и перенести и принять мой вопрос, если считаете, что он исправил исходную проблему. –
Теперь я загрузил новый вопрос, я обновлю это, чтобы вернуться в исходную область. –