Removing backslashes from a string in Python

大城市里の小女人 提交于 2019-12-17 21:00:46

问题


How do I remove all the backslashes from a string in Python?

This is not working for me:

result = result.replace("\\", result)

Do I need to treat result as a raw string?


回答1:


Your code is saying to replace each instance of '\' with result. Have you tried changing it to result.replace("\\", "") ?




回答2:


Use decode('string_escape'), for example:

result = stringwithbackslashes.decode('string_escape')

string_escape : Produce a string that is suitable as string literal in Python source code

or just:

result.replace("\\", "") 



回答3:


result = result.replace("\\", "")



回答4:


If you are interested in seeing the component words that are being separated by the '\' character, use:

result.replace('\\', ' ')


来源:https://stackoverflow.com/questions/3160752/removing-backslashes-from-a-string-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!