Reputation: 1614
Say I have a json file like
{"text":"Here is a string\nhere is another string","timestamp":"2021-05-24"}
I am trying to load it to a file and write the text field down, so the output file will be exactly
Here is a string\nhere is another string
However, if I read it as a json and do something like out.write(j['text'])
, I will get
Here is a string
here is another string
in the file, which translates \n
into a new line. Is there a way I could output the string in the desired way?
Upvotes: 0
Views: 1110
Reputation: 366
Yes, however you would need to escape the \
in your string. Try using str.replace('\n', '\\n')
. However, this would not work in cases where you would want to have \n
somewhere else in the string.
Upvotes: 1