本文介绍了Python zlib输出,如何从mysql utf-8表中恢复?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在python中,我使用zlib压缩了一个字符串,然后使用utf-8编码将其插入到blob类型的mysql列中.该字符串以utf-8的形式返回,但尚不清楚如何将其恢复为可以解压缩的格式.这是一些pseduo输出:

In python, I compressed a string using zlib, and then inserted it into a mysql column that is of type blob, using the utf-8 encoding. The string comes back as utf-8, but it's not clear how to get it back into a format where I can decompress it. Here is some pseduo-output:

valueInserted = zlib.compress('a')='x \ x9cK \ x04 \ x00 \ x00b \ x00b'

valueInserted = zlib.compress('a')= 'x\x9cK\x04\x00\x00b\x00b'

valueFromSqlColumn= u'x \ x9cK \ x04 \ x00 \ x00b \ x00b'

valueFromSqlColumn= u'x\x9cK\x04\x00\x00b\x00b'

zlib.decompress(valueFromSqlColumn)UnicodeEncodeError:'ascii'编解码器无法在位置1处编码字符u'\ x9c':序数不在范围内(128)

zlib.decompress(valueFromSqlColumn)UnicodeEncodeError: 'ascii' codec can't encode character u'\x9c' in position 1: ordinal not in range(128)

如果我这样做,它会插入一些额外的字符:

if i do this, it inserts some extra characters:

valueFromSqlColumn.encode('utf-8')='x \ xc2 \ x9cK \ x04 \ x00 \ x00b \ x00b'

valueFromSqlColumn.encode('utf-8')= 'x\xc2\x9cK\x04\x00\x00b\x00b'

有什么建议吗?

推荐答案

Unicode旨在与latin-1兼容,因此请尝试:

Unicode is designed to be compatible with latin-1, so try:

>>> import zlib
>>> u = zlib.compress("test").decode('latin1')
>>> u
u'x\x9c+I-.\x01\x00\x04]\x01\xc1'

然后

>>> zlib.decompress(u.encode('latin1'))
'test'

固定错字,latin-1并非与unicode兼容,反之亦然.

Fixed typo, latin-1 isn't designed to be compatible with unicode, it's the other way around.

这篇关于Python zlib输出,如何从mysql utf-8表中恢复?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 07:32