Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
替换python中的特殊字符串_Python_String_Replace - Fatal编程技术网

替换python中的特殊字符串

替换python中的特殊字符串,python,string,replace,Python,String,Replace,如何使用替换来消除字符串“b”\n 由于反斜杠是一个特殊的字符,我有一些问题 使用带有特殊字符\\的a来捕捉反斜杠: 来自重新导入子系统的 测试字符串=“ab”\ncdef” sub(pattern=“b'\\n”,repl=“-foundit-”,string=test\u string) >>>“a-找到它-cdef” 尝试使用替换方法 >>> # The 'r' prefix indicates it is a raw string >>> string

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

由于反斜杠是一个特殊的字符,我有一些问题

使用带有特殊字符
\\
的a来捕捉反斜杠:

来自重新导入子系统的

测试字符串=“ab”\ncdef”
sub(pattern=“b'\\n”,repl=“-foundit-”,string=test\u string)
>>>“a-找到它-cdef”

尝试使用替换方法

>>> # 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
希望这有帮助

参考资料:


请给出一个例子来说明具体问题。我也希望这样做,但在数据帧的一列中。