简体   繁体   中英

unable to decode this string using python

I have this text.ucs file which I am trying to decode using python.

file = open('text.ucs', 'r')
content = file.read()
print content

My result is

\\xf\\xe\\x002\\22

I tried doing decoding with utf-16, utf-8

content.decode('utf-16')

and getting error

Traceback (most recent call last): File "", line 1, in File "C:\\Python27\\lib\\encodings\\utf_16.py", line 16, in decode return codecs.utf_16_decode(input, errors, True) UnicodeDecodeError: 'utf16' codec can't decode bytes in position 32-33: illegal encoding

Please let me know if I am missing anything or my approach is wrong

Edit: Screenshot has been asked 在此处输入图片说明

字符串被编码为UTF16-BE(大端),可以这样工作:

content.decode("utf-16-be")

oooh, as i understand you using python 2.xx but encoding parameter was added only in python 3.xx as I know, i am doesn't master of python 2.xx but you can search in google about io.open for example try:

file = io.open('text.usc', 'r',encoding='utf-8')
content = file.read()
print content

but chek do you need import io module or not

You can specify which encoding to use with the encoding argument:

with open('text.ucs', 'r', encoding='utf-16') as f:
    text = f.read()

your string need to Be Uncoded With The Coding utf-8 you can do What I Did Now for decode your string

f = open('text.usc', 'r',encoding='utf-8')
print f

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM