无法在 python 中解码西里尔文字符串

Ivr*_*vri 3 python encoding

我有一个带有字符串的编码文件,例如

b'1'    b'\xca\xee\xef\xe5\xe9\xf1\xea' b'1'    b'ADMIN'    b'2013-07-08 00:21:55'  
b'2'    b'\xd7\xe5\xeb\xff\xe1\xe8\xed\xf1\xea' b'1'    b'ADMIN'    b'2013-07-08 00:22:05'  
Run Code Online (Sandbox Code Playgroud)

我应该如何解码它?我尝试使用编解码器,对 cp1251 进行解码/编码,但没有奏效。

file -bi 说 charset=us-ascii

西里尔文(cp1251)实际上应该有一个字符串

蟒蛇 2.7

输出:

>>> w=r'\xd7\xe5\xe\xff\xe1\xe8\xed\xf1\xea'
>>> w='\xd7\xe5\xe\xff\xe1\xe8\xed\xf1\xea'
ValueError: invalid \x escape
>>> w=r'\xd7\xe5\xe\xff\xe1\xe8\xed\xf1\xea'
>>> w.decode('raw_unicode_escape')
u'\\xd7\\xe5\\xe\\xff\\xe1\\xe8\\xed\\xf1\\xea'
>>> w.decode('utf-8')
u'\\xd7\\xe5\\xe\\xff\\xe1\\xe8\\xed\\xf1\\xea'
>>> unicode(w)
u'\\xd7\\xe5\\xe\\xff\\xe1\\xe8\\xed\\xf1\\xea'
>>> unicode(w, 'utf-8')
u'\\xd7\\xe5\\xe\\xff\\xe1\\xe8\\xed\\xf1\\xea'
Run Code Online (Sandbox Code Playgroud)

我做了一切:解码(“utf-8”),使用unicode等等,但没有任何变化。每次我得到相同的字节集。

b4h*_*and 5

问题是当您的变量显示无效转义时,您在变量中b的第 3 次\x转义之后丢失了w

>>> w = '\xd7\xe5\xeb\xff\xe1\xe8\xed\xf1\xea'
>>> w.decode('cp1251')
u'\u0427\u0435\u043b\u044f\u0431\u0438\u043d\u0441\u043a'
Run Code Online (Sandbox Code Playgroud)

  • 谢谢!这有效:打印 w.decode('cp1251').encode('utf-8')=> Челябинск (3认同)