2

I'm trying to save a text from python but it generates strange characters "\u00f1" and I don't know how to eliminate this

This is the code I'm using:

with open("NoticiasHabboHotel.json", "w") as f:
        json.dump(test_versions,   f, indent=0, separators=(',', ': '))

This is the text that generates me:

Dise\u00f1adores de salas, \u00a1os necesitamos!27345

What I want is to convert it into normal letters

I have tried different methods, such as encoding="utf8" and it doesn't work

Could someone help me, thank you very much!

  • 4
    Please translate your title to English, since Stack Overflow is an English-only site. – Pranav Hosangadi Nov 23 '22 at 16:28
  • JSON consists of ASCII characters. Any characters that aren't ASCII have to be encoded using the `\u` escape for Unicode code points, it won't show those characters legibly. But when you load the JSON later and print the string, you should see the letters you want. – Barmar Nov 23 '22 at 16:33
  • Does this answer your question? [Python encoding and json dumps](https://stackoverflow.com/questions/35582528/python-encoding-and-json-dumps) – Joachim Sauer Nov 23 '22 at 16:34
  • Have you bothered reading the documentation of json.dump? Quote: If ensure_ascii is true (**the default**), the output is guaranteed to have all incoming non-ASCII characters escaped. If ensure_ascii is false, these characters will be output as-is. – treuss Nov 23 '22 at 16:35
  • @Barmar JSON is specified to use UTF-8, not ASCII, see https://www.rfc-editor.org/rfc/rfc8259#section-8.1 – Mark Rotteveel Nov 24 '22 at 12:10

1 Answers1

2

See: https://docs.python.org/3/library/json.html#basic-usage

You need to add ensure_ascii=False because ñ is a non-ASCII character

with open("NoticiasHabboHotel.json", "w") as f:
        json.dump(test_versions,   f, indent=0, separators=(',', ': '), ensure_ascii=False)
riigs
  • 111
  • 8