基本写法
import json
with open("data.json", "r", encoding="utf-8") as f:
data = json.load(f)
print(type(data)) # 一般是 dict 或 list
print(data)
2026/1/25小于 1 分钟
import json
with open("data.json", "r", encoding="utf-8") as f:
data = json.load(f)
print(type(data)) # 一般是 dict 或 list
print(data)
import json
def read_jsonl(path):
data = []
with open(path, "r", encoding="utf-8") as f:
for line in f:
line = line.strip()
if not line:
continue
data.append(json.loads(line))
return data
# 用法
records = read_jsonl("data.jsonl")
print(len(records))
print(records[0])
import json
data = {
"name": "Alice",
"age": 25,
"skills": ["Python", "AI", "RAG"],
"active": True
}
with open("data.json", "w", encoding="utf-8") as f:
json.dump(data, f, ensure_ascii=False, indent=2)