I want to be able to use backslashes in string literals, for example: C:\file.txt
I was expecting to be able to escape the backslash with "\\", but for some weird reason that produces two backslashes instead of one. Why?
MVCE:
with open("temp.csv", 'w', encoding='utf-8') as f:
d = { "filepath" : "C:\\bahä.txt" }
s = json.dumps(d, ensure_ascii=False)
f.write(s)
Expected behavior: file content should be C:\bahä.txt
Actual behavior: file content is C:\\bahä.txt
In JSON you need to escape the backslash. That is why when you dump your string it keeps the backslash escaped.
if you have only one backslash, e.g. "C:\bahä.txt" then \b is backspace (i.e. if it wasn't b after the backslash to produce valid escape sequence it would be invalid json).
you can test and see that
import json
with open("temp.json", 'w', encoding='utf-8') as f:
d = {"filepath":"\ "}
s = json.dumps(d, ensure_ascii=False)
f.write(s)
or
import json
with open("temp.json", 'w', encoding='utf-8') as f:
d = { "filepath" : r"\b" }
s = json.dumps(d, ensure_ascii=False)
f.write(s)
both will again produce escaped backslash (i.e. \\) in the resulting json.
https://stackoverflow.com/a/19176131/4046632
As a side note - you are writing json, use the proper extension - .json, not .csv
The problem here is that you are serializing to JSON.
In Json some special characters are serialized with a \ prepended: https://stackoverflow.com/a/19176131/551045
So a \ is always \\ in proper serialized JSON data.
If you change your code to:
with open("temp.csv", 'w', encoding='utf-8') as f:
d = "C:\\bah.txt"
f.write(d)
You'll see that your file will only contain one slash.
BTW that's why we always ask for MVCE even if the problem seems "trivial".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With