Python3 JSON

Serialising to and from JSON, handling datetime and Decimal, and when to use csv or pickle.

Beginner
Key Points`ensure_ascii=False` keeps Chinese readable; datetime and Decimal need an explicit default= encoder.

dumps / loads

Python
import json

data = {"id": 1, "name": "后端", "tags": ["api", "py"]}

text = json.dumps(data, ensure_ascii=False, indent=2)
print(text)
# {
#   "id": 1,
#   "name": "后端",
#   "tags": ["api", "py"]
# }

back = json.loads(text)
print(back["name"])          # 后端
FunctionDirectionTarget
json.dumps(obj)Python -> strString
json.loads(s)str -> PythonObject
json.dump(obj, f)Python -> fileFile object
json.load(f)file -> PythonObject

Non-serialisable Types

datetime, Decimal, set and custom objects are not JSON types. Provide default= for encoding and object_hook/parse_float for decoding.

Python
import json
from datetime import datetime, date
from decimal import Decimal


def encode(o):
    if isinstance(o, (datetime, date)):
        return o.isoformat()
    if isinstance(o, Decimal):
        return str(o)          # 金额用字符串,避免精度丢失
    if isinstance(o, set):
        return sorted(o)
    raise TypeError(f"not JSON serialisable: {type(o)}")


payload = {"at": datetime(2026, 1, 1, 12, 0), "price": Decimal("19.90")}
print(json.dumps(payload, default=encode, ensure_ascii=False))
# {"at": "2026-01-01T12:00:00", "price": "19.90"}

csv

Python
import csv

with open("users.csv", encoding="utf-8", newline="") as f:
    for row in csv.DictReader(f):        # 按表头映射成字典
        print(row["name"], row["age"])

with open("out.csv", "w", encoding="utf-8", newline="") as f:
    w = csv.DictWriter(f, fieldnames=["name", "age"])
    w.writeheader()
    w.writerows([{"name": "alice", "age": 30}])

pickle

pickle serialises arbitrary Python objects, but it is not safe to load untrusted data — unpickling can execute code. Use it for local caches only.

Python
import pickle

blob = pickle.dumps({"a": 1})
pickle.loads(blob)          # {'a': 1}

# 跨语言 / 跨版本 / 公开接口:用 JSON
json.dumps with ensure_ascii=True (the default) turns 后端 into \u540e\u7aef. Always pass ensure_ascii=False for Chinese content.

Friends & Partners