-1

如何使用替换来消除字符串:“b'\n”?

由于反斜杠是一个特殊字符,我遇到了一些问题。

4

2 回答 2

1

尝试使用替换方法。

>>> # 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

希望这可以帮助

参考:

于 2020-01-21T12:41:12.257 回答
1

使用带有特殊字符的正则表达式\\来捕获反斜杠:

from re import sub

test_string = "ab'\ncdef"
sub(pattern = "b'\\n", repl = " - found it - ", string = test_string)

>>> 'a - found it - cdef'
于 2020-01-21T12:10:00.017 回答