首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python中的取消转义字符串

Python中的取消转义字符串
EN

Stack Overflow用户
提问于 2016-06-08 03:15:29
回答 2查看 667关注 0票数 2

我有一个输入文件,其中包含一个输入列表,每一行一个。每一行输入都用双引号括起来。输入有时有反斜杠或几个双引号,如包围双引号(请参阅下面的示例)。

样本输入-

代码语言:javascript
复制
"each line is enclosed in double-quotes"
"Double quotes inside a \"double-quoted\" string!"
"This line contains backslashes \\not so cool\\"
"too many double-quotes in a line \"\"\"too much\"\"\""
"too many backslashes \\\\\\\"horrible\"\\\\\\"

我想接受上面的输入,并简单地将行中转义双引号的输入转换为回勾`。

我假设有一个简单的单行解决方案。我试过以下几种方法,但不起作用。任何其他一条龙解决方案或对以下代码的修复将不胜感激。

代码语言:javascript
复制
def fix(line):
    return re.sub(r'\\"', '`', line)

它对输入第3行和第5行失败。

代码语言:javascript
复制
"each line is enclosed in double-quotes"
"Double quotes inside a `double-quoted` string!"
"This line contains backslashes \\not so cool\`
"too many double-quotes in a line ```too much```"
"too many backslashes \\\\\\`horrible`\\\\\`

我能想到的任何解决办法都会打断其他线路。请帮帮我!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-06-08 03:26:14

这并不完全是您所要求的,因为它代替了"而不是, but I'll mention it ... you could always leverage offcsvto do\‘转换:

代码语言:javascript
复制
>>> for line in csv.reader(["each line is enclosed in double-quotes",
...                         "Double quotes inside a \"double-quoted\" string!",
...                         "This line contains backslashes \\not so cool\\",
...                         "too many double-quotes in a line \"\"\"too much\"\"\"",
...                         "too many backslashes \\\\\\\"horrible\"\\\\\\",
...                         ]):
...         print(line)
...     
['each line is enclosed in double-quotes']
['Double quotes inside a "double-quoted" string!']
['This line contains backslashes \\not so cool\\']
['too many double-quotes in a line """too much"""']
['too many backslashes \\\\\\"horrible"\\\\\\']

如果它们是实际的's, you could simply do a replace on the text returned by thecsv` `模块很重要的话。

票数 2
EN

Stack Overflow用户

发布于 2016-06-08 03:20:40

在反斜杠后添加+

代码语言:javascript
复制
return re.sub(r'\\+"', '`', line)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37692629

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档