0

I'm reading a line from a file, which represents a string. I.E reading from file: \'\\000\\000\\000\\000\'. When printing the text from the file, python prints \'\\000\\000\\000\\000\' (without considering the \ etc.).

But when writing this text explicitly on the code and then printing it, python prints '\000\000\000\000' (as expected).

I want to convert the string from the file to be treated as if it was explicitly written in the code (I'm not sure about the definitions). I tried to decode it but it changes the text entirely (adds few more slashes etc.)

with open('f.txt', 'r') as f:
    line = f.readline() # line is written on the file \'\\000\\000\\000\\000\'
    print(line) # prints \'\\000\\000\\000\\000\'
    explicit_line = "\'\\000\\000\\000\\000\'"
    print(explicit_line) # prints '\000\000\000\000'
    # I would like print(line) to print the same content as print(explicit_line)

Any ideas? Thanks!

Gal Shahar
  • 2,695
  • 1
  • 21
  • 29
  • Does this help? https://stackoverflow.com/questions/1885181/how-to-un-escape-a-backslash-escaped-string – Dion May 12 '20 at 11:44

2 Answers2

2

Maybe you can try str.decode('unicode-escape')

Full code:

with open('f.txt', 'r') as f:
    line = f.readline() # line is written on the file \'\\000\\000\\000\\000\'

    print(line.encode('latin1').decode('unicode-escape')) # !!! here is the key point!!!

    print(line)
    explicit_line = "\'\\000\\000\\000\\000\'"
    print(explicit_line)

str.encode('latin1')is required here for converting str to bytes first, or you can't use decode('unicode-escape')

Ter
  • 90
  • 7
1

You can try python raw string which doesn't escape backslash

print(r'\\0000...')

raw_str = r'\abc\\a\\000'
print(raw_str)
\abc\\a\\000
Zabir Al Nazi
  • 10,298
  • 4
  • 33
  • 60