Python - Printing Hex from a file -
i have following code:
code1 = ("\xd9\xf6\xd9\x74\x24\xf4\x5f\x29\xc9\xbd\x69\xd1\xbb\x18\xb1") print code1 code2 = open("code.txt", 'rb').read() print code2
code1 output:
�צ�t$פ_)�½i�»±
code2 output:
"\xd9\xf6\xd9\x74\x24\xf4\x5f\x29\xc9\xbd\x69\xd1\xbb\x18\xb1"
i need code2 (which read file) have same output code1.
how can solve ?
to interpret sequence of characters such as
in [125]: list(code2[:8]) out[125]: ['\\', 'x', 'd', '9', '\\', 'x', 'f', '6']
as python string escaped characters, such as
in [132]: list('\xd9\xf6') out[132]: ['\xd9', '\xf6']
use .decode('string_escape')
:
in [122]: code2.decode('string_escape') out[122]: '\xd9\xf6\xd9t$\xf4_)\xc9\xbdi\xd1\xbb\x18\xb1'
in python3, string_escape
codec has been removed, equivalent becomes
import codecs codecs.escape_decode(code2)[0]
Comments
Post a Comment