我有一個包含輸入列表的輸入文件,每行一個。每行輸入都用雙引號括起來。輸入有時會在封閉的雙引號內使用反斜槓或少量雙引號(請查看下面的示例)。Python中的Unescape字符串
樣品輸入 -
"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)
它未能對輸入線和。
"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`\\\\\`
我能想到的任何修補程序都會打破其他行。請幫忙!
輸入行仍然中斷* 3 * – Bala