gpt4 book ai didi

python - 从 Python 3.4 中的字符串中删除非 utf-8 字符

转载 作者:行者123 更新时间:2023-11-29 22:08:22 24 4
gpt4 key购买 nike

我正在尝试从 MySQL 检索一些数据,但读取数据时遇到问题。列数据类型为 utf8_general-ci 的 varchar。我尝试解码它,但它不起作用。因此,我想删除那些非 utf8 字符,因为我不需要那些非 utf8 字符。

#This is the line causing the problem:
line: ((123, 'Classical Musicï¼\x8c', 69),)

conn = db.cursor()
conn.execute(sql)
data = conn.fetchall()
for line in data:
for x in line:
print(x)

收到错误代码

UnicodeEncodeError: 'charmap' codec can't encode character '\x8c' in position 17

我已尝试解码('utf-8'),但出现另一个错误。

conn = db.cursor()
conn.execute(sql)
data = conn.fetchall()
for line in data:
for x in line:
print(x[1].decode('utf-8'))

AttributeError: 'str' object has no attribute 'decode'

最佳答案

Mojibake 和双重编码,以及 Python 的修改。

重新开始。将所有内容设为 utf8 —— 文本、连接、字符集、html header 。

如果还有问题,请回来;希望您的代码足够接近以纠正我们开出治疗方法。

同时,请阅读此处的更多主题;更简单的困惑版本比比皆是。

C3AF C2BB C2BF 应该是一个奇特的逗号,对吗? utf8 十六进制应该是 EFBC8C。该逗号是由什么过程生成的?

关于python - 从 Python 3.4 中的字符串中删除非 utf-8 字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31936234/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com