Removing backslashes from a string in Python

前端 未结 4 1946
挽巷
挽巷 2020-12-11 14:33

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

This is not working for me:

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

相关标签:
4条回答
  • 2020-12-11 15:08

    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("\\", "") 
    
    0 讨论(0)
  • 2020-12-11 15:10

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

    result.replace('\\', ' ')
    
    0 讨论(0)
  • 2020-12-11 15:16
    result = result.replace("\\", "")
    
    0 讨论(0)
  • 2020-12-11 15:17

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

    0 讨论(0)
提交回复
热议问题