Suppose there is a file in which a line like this is written:
"Hello World!\n" I read this line from the file as is, in the variable it turns out that:
'"Hello World!\\n"' How can I most easily convert this line to the original "normal" view (open quotes, open escape sequences) without using eval ?
Opening quotes does not cause any difficulties, in principle, there is also a solution for escape sequences (roughly speaking, as long as the line contains something from '\\n' , '\\r' , '\\t' , make the appropriate substitutions), but I would like to maximize the simple / short solution without non-standard dependencies (like parse ).
Need a solution for python3.
For python2, '"Hello World!\\n"'.strip('"').decode("string-escape") , but under python3, the string does not have the decode method, and the decode method of the bytes class does not reveal the escape sequence (or am I doing something wrong).
print((b'%s' % line).decode('unicode_escape'))? Your line formatted as it should - BOPOHbytes(line, 'utf-8').decode('unicode_escape')in Python 3.2.3 works fine - BOPOHline.encode('cp1252', 'backslashreplace').decode('unicode-escape')appropriate? for Russian it seems to work - BOPOH