0

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.

Community
  • 1
  • 1
Dev.K.
  • 2,428
  • 5
  • 35
  • 49
  • You have the *literal text* ``\``, `2`, `5`, `1`, so four characters. What kind of file format do you have? Is it RTF text or something similar perhaps? – Martijn Pieters Feb 03 '15 at 12:38

1 Answers1

2

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
Mazdak
  • 105,000
  • 18
  • 159
  • 188
  • Nope, you are correct, the text contains the literal text. – Martijn Pieters Feb 03 '15 at 12:37
  • @MartijnPieters , he write the text above of the code ! ;) – Mazdak Feb 03 '15 at 12:38
  • Got this error :ActivePython 2.7.5.6 (ActiveState Software Inc.) based on Python 2.7.5 (default, Sep 16 2013, 23:16:52) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> print unicode('PREFIX\251SUFFIX','unicode-escape') Traceback (most recent call last): File "", line 1, in File "C:\Python27\lib\encodings\cp437.py", line 12, in encode return codecs.charmap_encode(input,errors,encoding_map) UnicodeEncodeError: 'charmap' codec can't encode character u'\xa9' in position 6: character maps to – Dev.K. Feb 03 '15 at 12:42