如何在python中用空字符串替换反斜杠字符 [英] how to replace back slash character with empty string in python

查看:121
本文介绍了如何在python中用空字符串替换反斜杠字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用以下代码替换字符串中的反斜杠\"

string = "<P style='TEXT-INDENT'>\B7 </P>"result = string.replace("\",'')

结果:

------------------------------------------------------------文件<ipython console>",第 1 行result = string.replace("\",'')^语法错误:扫描字符串文字时 EOL

这里我不需要反斜杠,因为实际上我正在解析一个带有上述格式标签的 xml 文件,所以如果存在反斜杠,它会在解析过程中显示 invalid token>

我可以知道如何在python中用空字符串替换反斜杠

解决方案

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

I am trying replace a backslash '\' in a string with the following code

string = "<P style='TEXT-INDENT'>\B7 </P>"

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

result:

------------------------------------------------------------
   File "<ipython console>", line 1
     result = string.replace("\",'')
                                     ^
SyntaxError: EOL while scanning string literal

Here i don't need the back slashes because actually i am parsing an xml file which has a tag in the above format, so if backslashes are there it is displaying invalid token during parsing

Can i know how to replace the backslashes with empty string in python

解决方案

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

这篇关于如何在python中用空字符串替换反斜杠字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆