I have a string like "how "are" you", I want to replace quotes which are inside the quotes surrounding "are" with \" using regex in python
input_file = 'D:/Extracts/yourFileName.csv'
file_output= 'D:/Extracts/yourFileName_out.csv'
with open(input_file, 'r',encoding="utf8") as f, open(file_output, 'w',encoding="utf8") as fo:
for line in f:
fo.write(line.replace('"', '\"').replace(""", ""))
I want the output like "how \"are\" you"
The strings '\"'
and '"'
are identical: they both are just a single double-quote, because the sequence \"
encodes a double-quote character (usually for use in "
-quoted strings).
If you really want a backslash before the quote, you'll have to escape it:
fo.write(line.replace('"', '\\"'))
In order to not replace the first and last character, you can instead use regular expressions:
fo.write(re.sub('(?<!^)"(?!$)', '\\"', line))
The regular expression consists of a negative lookbehind ( (?<!^)
; asserting that no line starts before the quote), the quote character itself, and a negative lookahead ( (?!$)
; asserting that no line ends after the quote).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.