简体   繁体   中英

Python Conver Octal Escaped String to Data

My code :

'''
Content of Hello.txt is  only ->    \251

Actually \251 represents ©

'''

prefix = "PREFIX"
suffix = "SUFFIX"

f = open('Hello.txt')
d = f.read()
f.close()
print prefix+d+suffix 

My Problem is,this prints PREFIX\\251SUFFIX but I actually want it to print PREFIX©SUFFIX

As per How to convert octal escape sequences with Python I tried to d.decode('utf-8'), But still the same problem.

You need to clarify your string as unicode for python , you can use unicode function as following :

>>> print unicode('PREFIX\251SUFFIX','unicode-escape')
PREFIX©SUFFIX

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