一个奇怪的python unicode解码错误试图做redis转储(更喜欢python3,但在2和3中看到))

时间:2017-11-22 12:04:35

标签: python unicode encoding utf-8 ascii

我正在尝试使用python包redis-dump-load进行redis转储。

它在UTF-8,除了显然是一个键,我被告知在ascii。不知道为什么,但我想,好吧,如果这一个密钥有一个UnicodeDecodeError(我从转储流中收到大量数据到目前为止),我要做的就是得到编码字符串我有,并用它解码。

但是,我仍然可以获得UnicodeDecodeError ASCII!它是bytes,它是ascii,我认为它可能只是腐败而且我打算跳过它,但好奇是否有人有任何其他想法。

这是我的代码段:

value = {}
for k in response:
    try:
        value[k.decode(encoding)] = response[k].decode(encoding)
    except UnicodeDecodeError:
        print("Error for", k)
        print(type(k))
        orig_encoding = chardet.detect(k)['encoding']
        print(orig_encoding)
        value[k.decode(orig_encoding)] = response[k].decode(orig_encoding)
return value

这是我看到的输出:

Error for b'meta'
<class 'bytes'>
ascii
Traceback (most recent call last):
  File "redisdl.py", line 243, in handle_response
    value[k.decode(encoding)] = response[k].decode(encoding)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 0: invalid start byte

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "redisdl.py", line 635, in <module>
    main()
  File "redisdl.py", line 626, in main
    do_dump(options)
  File "redisdl.py", line 547, in do_dump
    dump(output, **kwargs)
  File "redisdl.py", line 174, in dump
    for key, type, ttl, value in _reader(r, pretty, encoding, keys):
  File "redisdl.py", line 293, in _reader
    type, ttl, value = _read_key(encoded_key, r, pretty, encoding)
  File "redisdl.py", line 284, in _read_key
    value = reader.handle_response(results[2], pretty, encoding)
  File "redisdl.py", line 250, in handle_response
    response[k].decode(orig_encoding)
UnicodeDecodeError: 'ascii' codec can't decode byte 0x80 in position 0: ordinal not in range(128)

我错过了什么吗?我已经查看了多个SO答案,相信我,直到现在我才开始理解strbytes,解码和编码。但也许不是。甚至py2&amp;我得到的py3差异。我认为。开始怀疑一切......

0 个答案:

没有答案