Reputation: 1014
I want to replace double backslashes to single one for byte string in Python.
For example, there is a bytes string.
word = b'Z\xa6\x97\x86j2\x08q\\r\xca\xe6m'
I need this bytes string.
word = b'Z\xa6\x97\x86j2\x08q\r\xca\xe6m'
If I use replace like:
word = word.replace(b"\\",b"\")
I got this error.
File "test.py", line 79
word = word.replace(b"\\", b"\")
^
SyntaxError: EOL while scanning string literal
Does anyone know how to do it?
Upvotes: 3
Views: 6963
Reputation: 378
You have a byte stream. You need to escape '\' and decode bytes.
word = b'Z\xa6\x97\x86j2\x08q\\r\xca\xe6m'
new_word = (str(word).encode('utf-8'))
print(new_word.replace(b"\\\\",b"\\").decode('ascii'))
Upvotes: 0
Reputation: 2817
\\
is not double backslash but one escaped. Look:
print b'Z\xa6\x97\x86j2\x08q\\r\xca\xe6m'
# Z���jq\r��m
And \r
(from your desired output) is not 2 chars but one:
print b'Z\xa6\x97\x86j2\x08q\r\xca\xe6m'
# ��m�jq
(When printing it to terminal, carriage return \r
prevents us from seen the first letter Z
)
If you really want to replace '\\r'
with '\r'
, you can do:
print repr(word.replace('\\r', '\r'))
# 'Z\xa6\x97\x86j2\x08q\r\xca\xe6m'
print word.replace('\\r', '\r')
# ��m�jq
Or, if you want to replace all the escape sequences. Python2 version:
print repr(b'1\\t2\\n3'.decode('string_escape'))
# '1\t2\n3'
print b'1\\t2\\n3'.decode('string_escape')
# 1 2
# 3
Python3 version:
print(repr(b'1\\t2\\n3'.decode('unicode_escape')))
# '1\t2\n3'
print(b'1\\t2\\n3'.decode('unicode_escape'))
# 1 2
# 3
Upvotes: 6
Reputation: 140307
your \r
is a carriage return character. So \\r
is \
plus carriage return. You won't find \\
in your string.
What "works" is to replace backslash+CR by just CR:
word = b'Z\xa6\x97\x86j2\x08q\\r\xca\xe6m'
print(word.replace(b"\\r",b"\r"))
result:
b'Z\xa6\x97\x86j2\x08q\r\xca\xe6m'
but I'm not sure that's what you meant from the start (that is: inserting a carriage return char in your bytes string)
Upvotes: 1