Golden Codes - armanexplorer planet

Practical code snippets for Django, Python, Bash, Git and All!

View on GitHub

store utf-8 characters to be readable as text too

When you use json.dump(data, f), the data could be ready by json.load(f). But if you want to be able to also read the data using text editors, you should care of the utf-8 chars.

To ensure proper handling of German characters as an example with umlauts (ä, ö, ü), you need to specify the encoding during the dumping process. Here's how:

1. Use the ensure_ascii=False argument:

import json

data = {"Nachricht": "Hallo Welt!"}  # German message

with open("data.json", "w", encoding="utf-8") as f:
  json.dump(data, f, ensure_ascii=False)

In this example:

Important: