Reputation: 42
I checked all Stackoverflow questions on this matter and none can answer my problem.I need to convert \\
to \
.
Edited:
This is what I am trying:
>>> a = b'\xe5jb\x8c?Q$\xf3\x1d\x97^\xfa3O\xa6U.txt'
>>> b = str(a)
>>> b
"b'\\xe5jb\\x8c?Q$\\xf3\\x1d\\x97^\\xfa3O\\xa6U.txt'"
>>> b = b.replace('b\'','')
>>> b = b[:len(b)-1]
>>> b
'\\xe5jb\\x8c?Q$\\xf3\\x1d\\x97^\\xfa3O\\xa6U.txt'
>>> c = bytes(b,'utf8')
>>> c
b'\\xe5jb\\x8c?Q$\\xf3\\x1d\\x97^\\xfa3O\\xa6U.txt'
>>> a == c
False
How do I make a==c
True? I tried
.replace("\\\\","\\")
but this doesn't help. The string remains the same. I need to store the byte in variable 'a' to a file as a text and call it back. Python-3.8, Windows=10
Upvotes: 2
Views: 236
Reputation: 106883
You can convert c
to a string with the decode
method, and then use ast.literal_eval
to evaluate it as a bytes literal after wrapping it with b'...'
:
from ast import literal_eval
a = b'\xe5jb\x8c?Q$\xf3\x1d\x97^\xfa3O\xa6U.txt'
c = b'\\xe5jb\\x8c?Q$\\xf3\\x1d\\x97^\\xfa3O\\xa6U.txt'
c = literal_eval("b'%s'" % c.decode())
print(a == c)
This outputs: True
Upvotes: 1