如何使用替换来消除字符串:“b'\n”?
由于反斜杠是一个特殊字符,我遇到了一些问题。
尝试使用替换方法。
>>> # The 'r' prefix indicates it is a raw string
>>> string = r"b'\n"
>>> print(string.replace(r"b'\n'", ""))
>>>
>>> # It will replace the pattern you wanted to a empty string
希望这可以帮助
参考:
使用带有特殊字符的正则表达式\\
来捕获反斜杠:
from re import sub
test_string = "ab'\ncdef"
sub(pattern = "b'\\n", repl = " - found it - ", string = test_string)
>>> 'a - found it - cdef'