I have an ASCII-encoded JSON file with unicode-escapes (e.g., \\u201cquotes\\u201d
) and newlines escaped within strings, (e.g., `"foo\\r\\nbar"
). Is there a simple way in Python to generate a utf-8 encoded file by un-escaping the unicode-escapes, but leaving the newline escapes intact?
Calling decode('unicode-escape')
on the string will decode the unicode escapes (which is what I want) but it will also decode the carriage returns and newlines (which I don't want).
Sure there is, use the right tool for the job and ask the
json
module to decode the data to Pythonunicode
; then encode the result to UTF-8:Use
unicode-escape
only for actual Python string literals. JSON strings are not the same as Python strings, even though they may, at first glance, look very similar.Short demo (take into account the python interactive interpreter echoes strings as literals):
Note that the JSON decoder decodes
\r
on\n
just like a python literal would.If you absolutely have to only process the
\uabcd
unicode literals in the JSON input but leave the rest intact, then you need to resort to a regular expression:which gives: