lawless-m avatar

python-json

UTF-8 JSON file I/O utilities to avoid Windows encoding issues (CP-1252 vs UTF-8)

提供方 lawless-m|开源

Python JSON

Python's open() uses the system encoding, which on Windows is CP-1252, not UTF-8 — so reading/writing JSON that contains £, , or accented letters produces mojibake and violates RFC 8259 (JSON must be UTF-8). Two rules fix it:

  • Always pass encoding='utf-8' when opening a JSON file.
  • Always pass ensure_ascii=False when writing, so Unicode stays readable instead of becoming \uXXXX.

Convention

Every Python project here that touches JSON gets a copy of json_io.py (in this skill directory) dropped into its utils/, then imports from it instead of calling json.load/json.dump directly:

from utils.json_io import load_json, save_json

data = load_json('config.json')
save_json('output.json', {'price': '£99.99'})

json_io.py is already correct — copy it, don't rewrite it.

Symptom → fix

  • £, €, é appearing in data → a UTF-8 file was read as CP-1252; add encoding='utf-8' (or use load_json).
  • £ in written output → json.dump defaulted to ensure_ascii=True; pass ensure_ascii=False (or use save_json).