[英]Unescape strings in Python
我有一個輸入文件,其中包含輸入列表,每行一個。 輸入的每一行都用雙引號引起來。 輸入有時會包含反斜杠或一些雙引號,如括在雙引號內(請參見下面的示例)。
樣本輸入—
"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\"\\\\\\"
我想接受以上輸入並將行中帶有轉義雙引號的輸入轉換為反引號` 。
我認為對此有一個簡單的單線解決方案。 我嘗試了以下操作,但不起作用。 任何其他單線解決方案或對以下代碼的修復將不勝感激。
def fix(line):
return re.sub(r'\\"', '`', line)
輸入線路3和5失敗。
"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`\\\\\`
我能想到的任何修復方法都會破壞其他方面。 請幫忙!
這不是您所要求的,因為它被替換為"
而不是`,但是我會提到它……您始終可以利用csv
為您正確地進行\\"
轉換:
>>> 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"\\\\\\']
如果很重要的一點是要讓它們成為實際的`,可以簡單地對csv
模塊返回的文本進行替換。
在反斜杠后添加+
。
return re.sub(r'\\+"', '`', line)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.