mysql3的python3编码 [英] python3 encoding for mysql database
问题描述
- 我发出了一个请求,请求以json utf-8(带有字典的字典)响应.
- 我想将json-response插入到mysql数据库中.
- 我使用
string.encode('utf-8')
对字符串进行编码,但随后也以UTF-8编码的数据库将不会插入正确的值.
- I make a request which responds in json utf-8 (dict with tupels).
- I'd like to insert the json-response into a mysql database.
- I encode the string with
string.encode('utf-8')
but then the Database, which encodes also in UTF-8 wont insert the correct value.
没有string.encode('utf-8')
,我将收到UnicodeEncodeError,因此似乎有必要对字符串进行编码
Without string.encode('utf-8')
i will get an UnicodeEncodeError, so it seems this necessary to encode the string
UnicodeEncodeError: 'ascii' codec can't encode character '\xfc' in position 147: ordinal not in range(128)
我举个例子:
- json中的字符串:quälend"
- 编码后的字符串:"qu \ xC3 \ xA4lend"
- 插入数据库:quälend"
我的猜测是,该数据库仍将编码的utf-8字符串处理为latin-1,但是当我插入一些未编码的示例utf-8字符串时,它就可以工作了.因此数据库可以正确处理它,但不能使用编码的字符串.
my guess is, that database handles the encoded utf-8 string still as latin-1, but when I insert some sample utf-8 string which is not encoded, then it works. So the database can handle it right, but just not with the encoded string.
您是否知道可能是什么问题?几个小时以来,我对这个问题失去了信心.任何提示都将受到高度赞赏.
Do you have an idea what the problem might be? I'm loosing my mind about this issue since hours. Any hint is highly appreciated.
推荐答案
请勿使用任何编码/解码功能;只会使情况变得更糟.
Do not use any encode/decode functions; that only makes things worse.
ä
在UTF-8中正确为C3A4
(MySQL的CHARACTER SET
utf8或utf8mb4).
ä
is correctly C3A4
in UTF-8 (MySQL's CHARACTER SET
utf8 or utf8mb4).
ä
是"Mojibake".搜索 http://stackoverflow.com /questions/38363566/trouble-with-utf8-characters-what-i-see-is-not-what-i-stored 是什么原因造成的,以及为避免这种情况您需要采取的措施.
ä
is "Mojibake". Search http://stackoverflow.com/questions/38363566/trouble-with-utf8-characters-what-i-see-is-not-what-i-stored for what causes that and what you needed to do to avoid it.
目前尚不清楚"ascii"和"latin-1"在哪里参与;避免他们. FC
是ü
的latin1.
It is unclear where "ascii" and "latin-1" got involved; avoid them. FC
is latin1 for ü
.
更多python注释: http://mysql.rjweb.org/doc.php /charcoll#python
More python notes: http://mysql.rjweb.org/doc.php/charcoll#python
这篇关于mysql3的python3编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!