I have a text file, and its content is like this:
"good to know it \" so nice \" "
I use Python to read its contents and want to replace " with an empty string.
The code I am using is:
import re
file_path = "backslash_double_quotation.txt"
with open(file_path, "r") as input_file:
raw_text = input_file.read()
processed_text = re.sub(r'\"', "", raw_text)
print(raw_text)
print(processed_text)
and I expect processed_text like this:
"good to know it so nice "
However, the actual output is:
good to know it \ so nice \
All the double quotations are replaced by empty strings. How can I fix this?
re.subtreatsr'\"'as a regular expression, and the regular expression\"only matches a literal"(as"has no special meaning in a regular expression).r'\"'would be correct if you using string equality, and not regular-expression matching.