简体   繁体   English

Python Conver八进制转义字符串到数据

[英]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 我的问题是,这会打印PREFIX \\ 251SUFFIX,但是我实际上希望它打印PREFIX©SUFFIX

As per How to convert octal escape sequences with Python I tried to d.decode('utf-8'), But still the same problem. 根据如何使用Python转换八进制转义序列,我尝试了d.decode('utf-8'),但仍然存在相同的问题。

You need to clarify your string as unicode for python , you can use unicode function as following : 您需要将字符串声明为python的unicode,您可以使用unicode函数,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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