[英]How to use replace double backslashes to single one for byte string in Python
I want to replace double backslashes to single one for byte string in Python. 我想在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? 有谁知道怎么做?
\\\\
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: \\r
(从你想要的输出)不是2个字符而是一个:
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
) (当它打印到终端时, 回车
\\r
阻止我们看到第一个字母Z
)
If you really want to replace '\\\\r'
with '\\r'
, you can do: 如果你真的想用
'\\\\r'
替换'\\\\r'
'\\r'
,你可以这样做:
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:
Python2版本:
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: Python3版本:
print(repr(b'1\\t2\\n3'.decode('unicode_escape')))
# '1\t2\n3'
print(b'1\\t2\\n3'.decode('unicode_escape'))
# 1 2
# 3
your \\r
is a carriage return character. 你的
\\r
是一个回车符。 So \\\\r
is \\
plus carriage return. 所以
\\\\r
是\\
加上回车。 You won't find \\\\
in your string. 你不会在你的字符串中找到
\\\\
。
What "works" is to replace backslash+CR by just CR: “工作”是用CR取代反斜杠+ 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) 但我不确定你从一开始就是什么意思(即:在你的字节字符串中插入回车符)
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'))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.