'charmap' 编解码器无法对位置 0 中的字符 '\u010c' 进行编码:字符映射到 <undefined> MariaDB 和 SQLAlchemy

'charmap' codec can't encode character '\u010c' in position 0: character maps to <undefined> MariaDB and SQLAlchemy

我正在尝试使用 SQLAlchemy 将数据插入到 MariaDB 数据库中。我正在解析一个 XML 文件以获取我需要插入的数据。我读取数据没问题。许多关于此错误的问题,例如 python 3.2 UnicodeEncodeError: 'charmap' codec can't encode character '\u2013' in position 9629: character maps to <undefined> 指的是从文件中读取数据。但是,我在这一步遇到了错误,

con_string = db_driver + '://' + db_user + ':' + db_password + '@' + db_host + ':' + db_port + '/' + db_name
engine = sqlalchemy.create_engine(con_string, pool_pre_ping=True)

meta_data = sqlalchemy.MetaData(engine)
table = sqlalchemy.Table('table', meta_data, autoload=True, autoload_with=engine)
table.insert().execute(data)

data 是字典。它包含从解析映射到 table.

的列的 XML 文件中获得的值

可能是什么原因?

为了支持所有 Unicode 字符,我们需要将 ?charset=utf8mb4 添加到连接 URL 的末尾,如

所述

https://docs.sqlalchemy.org/en/14/dialects/mysql.html#charset-selection

例如,

e = create_engine(
    "mysql+pymysql://scott:tiger@localhost/test?charset=utf8mb4")