Как декодировать этот вид байтов в Python 3.6 - PullRequest
0 голосов
/ 06 декабря 2018

Я получил этот byte_var из файла Apache error.log, я хочу декодировать его на некоторый читаемый китайский, но он не работает.

# escape_byte.py
# coding=utf-8
from __future__ import unicode_literals, absolute_import

import codecs

byte_var = b'[Mon Oct 29 02:00:43.097264 2018] [wsgi:error] [pid 5256:tid 1004] [client 192.168.80.1:53308] {\'kind\': \'youtube#video\', \'etag\': \'"XI7nbFXulYBIpL0ayR_gDh3eu1k/dFIjqowBYZNVQ5trPQpm7M94K7c"\', \'id\': \'rzGuVNqCqfA\', \'snippet\': {\'publishedAt\': \'2018-10-28T18:03:21.000Z\', \'channelId\': \'UCYrfvxfX4GjNf1BPxXCG_BQ\', \'title\': \'\\xe4\\xb8\\x89\\xe5\\x9b\\xbd\\xe8\\xb0\\x8b\\xe5\\xa3\\xab\\xe7\\xb3\\xbb\\xe5\\x88\\x97\\xe4\\xb9\\x8b\\xe4\\xb8\\x80\\xe4\\xb8\\x83\\xe4\\xb8\\x80\\xef\\xbc\\x9a\\xe9\\x83\\xad\\xe5\\x98\\x89\\xe8\\xaf\\xb4\\xe5\\x87\\xba\\xe8\\xbf\\x99\\xe7\\x95\\xaa\\xe8\\xaf\\x9d\\xef\\xbc\\x8c\\xe5\\x90\\x95\\xe5\\xb8\\x83\\xe6\\xb3\\xa8\\xe5\\xae\\x9a\\xe5\\x9c\\xa8\\xe5\\x8a\\xab\\xe9\\x9a\\xbe\\xe9\\x80\\x83\', \'description\': \'\\xe5\\x90\\xac\\xe5\\xae\\x8c\\xe9\\x83\\xad\\xe5\\x98\\x89\\xe7\\x9a\\x84\\xe5\\x88\\x86\\xe6\\x9e\\x90\\xef\\xbc\\x8c\\xe6\\x9b\\xb9\\xe6\\x93\\x8d\\xe9\\x9d\\x9e\\xe5\\xb8\\xb8\\xe7\\x9a\\x84\\xe8\\xb5\\x9e\\xe5\\x90\\x8c\\xef\\xbc\\x8c\\xe7\\xab\\x8b\\xe5\\x8d\\xb3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4\\xb8\\x8e\\xe9\\x83\\xad\\xe5\\x98\\x89\\xe6\\x98\\xaf\\xe9\\x9d\\x9e\\xe5\\xb8\\xb8\\xe4\\xb8\\x80\\xe8\\x87\\xb4\\xe7\\x9a\\x84\\xe3\\x80\\x82\\xe8\\xbf\\x99\\xe6\\xa0\\xb7\\xe4\\xb8\\x80\\xe6\\x9d\\xa5\\xef\\xbc\\x8c\\xe9\\x83\\xad\\xe5\\x98\\x89\\xe7\\x9a\\x84\\xe9\\xa6\\x96\\xe6\\xac\\xa1\\xe7\\x8c\\xae\\xe7\\xad\\x96\\xe5\\xb0\\xb1\\xe4\\xb8\\x8e\\xe6\\x9b\\xb9\\xe6\\x93\\x8d\\xe8\\xbe\\xbe\\xe6\\x88\\x90\\xe4\\xba\\x86\\xe9\\xbb\\x98\\xe5\\xa5\\x91\\xef\\xbc\\x8c\\xe8\\xbf\\x99\\xe6\\x97\\xa0\\xe8\\xae\\xba\\xe5\\xaf\\xb9\\xe9\\x83\\xad\\xe5\\x98\\x89\\xe6\\x8a\\x91\\xe6\\x88\\x96\\xe6\\x9b\\xb9\\xe6\\x93\\x8d\\xe8\\x80\\x8c\\xe8\\xa8\\x80\\xef\\xbc\\x8c\\xe9\\x83\\xbd\\xe6\\x98\\xaf\\xe4\\xb8\\x80\\xe4\\xb8\\xaa\\xe8\\x89\\xaf\\xe5\\xa5\\xbd\\xe7\\x9a\\x84\\xe5\\xbc\\x80\\xe7\\xab\\xaf\\xe3\\x80\\x82\', \'thumbnails\': {\'default\': {\'url\': \'https://i.ytimg.com/vi/rzGuVNqCqfA/default.jpg\', \'width\': 120, \'height\': 90}, \'medium\': {\'url\': \'https://i.ytimg.com/vi/rzGuVNqCqfA/mqdefault.jpg\', \'width\': 320, \'height\': 180}, \'high\': {\'url\': \'https://i.ytimg.com/vi/rzGuVNqCqfA/hqdefault.jpg\', \'width\': 480, \'height\': 360}}, \'channelTitle\': \'\\xe5\\x8e\\x86\\xe5\\x8f\\xb2\\xe8\\xb6\\xa3\\xe9\\x97\\xbb\', \'tags\': [\'\\xe6\\x9b\\xb9\\xe6\\x93\\x8d\', \'\\xe9\\x83\\xad\\xe5\\x98\\x89\', \'\\xe5\\x88\\x98\\xe5\\xa4\\x87\', \'\\xe5\\xbe\\x90\\xe5\\xb7\\x9e\', \'\\xe9\\x9b\\x86\\xe5\\x9b\\xa2\', \'\\xe5\\x90\\x95\\xe5\\xb8\\x83\', \'\\xe6\\x9d\\x80\\xe6\\x8e\\x89\', \'\\xe4\\xba\\xba\\xe6\\x89\\x8d\', \'\\xe9\\x83\\xad\\xe5\\x98\\x89\\xe6\\x98\\xaf\', \'\\xe6\\x94\\xbf\\xe6\\xb2\\xbb\\xe9\\x87\\x8e\\xe5\\xbf\\x83\'], \'categoryId\': \'22\', \'liveBroadcastContent\': \'none\', \'localized\': {\'title\': \'\\xe4\\xb8\\x89\\xe5\\x9b\\xbd\\xe8\\xb0\\x8b\\xe5\\xa3\\xab\\xe7\\xb3\\xbb\\xe5\\x88\\x97\\xe4\\xb9\\x8b\\xe4\\xb8\\x80\\xe4\\xb8\\x83\\xe4\\xb8\\x80\\xef\\xbc\\x9a\\xe9\\x83\\xad\\xe5\\x98\\x89\\xe8\\xaf\\xb4\\xe5\\x87\\xba\\xe8\\xbf\\x99\\xe7\\x95\\xaa\\xe8\\xaf\\x9d\\xef\\xbc\\x8c\\xe5\\x90\\x95\\xe5\\xb8\\x83\\xe6\\xb3\\xa8\\xe5\\xae\\x9a\\xe5\\x9c\\xa8\\xe5\\x8a\\xab\\xe9\\x9a\\xbe\\xe9\\x80\\x83\', \'description\': \'\\xe5\\x90\\xac\\xe5\\xae\\x8c\\xe9\\x83\\xad\\xe5\\x98\\x89\\xe7\\x9a\\x84\\xe5\\x88\\x86\\xe6\\x9e\\x90\\xef\\xbc\\x8c\\xe6\\x9b\\xb9\\xe6\\x93\\x8d\\xe9\\x9d\\x9e\\xe5\\xb8\\xb8\\xe7\\x9a\\x84\\xe8\\xb5\\x9e\\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\\x83\\xb3\\xe6\\xb3\\x95\\xe5\\x85\\xb6\\xe5\\xae\\x9e\\xe4\\xb8\\x8e\\xe9\\x83\\xad\\xe5\\x98\\x89\\xe6\\x98\\xaf\\xe9\\x9d\\x9e\\xe5\\xb8\\xb8\\xe4\\xb8\\x80\\xe8\\x87\\xb4\\xe7\\x9a\\x84\\xe3\r\n'

line_bytes = codecs.escape_decode(byte)[0]
line_unicode = line_bytes.decode('utf-8')
print(line_unicode)

информация об ошибке:

Traceback (most recent call last):
  File "G:/Python/Project/VideoCreation/article/test/escape_byte.py", line 10, in <module>
    line_unicode = line_bytes.decode('utf-8')
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe3 in position 2663: invalid continuation byte
...