import json
python_data = [
{'姓名': '张三', '年龄': '22', '住址': '广州', '职业': '程序员'},
{'姓名': '李四', '年龄': '23', '住址': '宁波', '职业': '工程师'},
{'姓名': '王二', '年龄': '25', '住址': '杭州', '职业': '工程师'},
{'姓名': '麻子', '年龄': '21', '住址': '', '职业': '教师'},
]
#### 将python_data写入data.json文件
with open('data.json', 'w', encoding='utf-8') as f:
json.dump(python_data, f, sort_keys=True, indent=4, ensure_ascii=False)
#### 将python_data转为string_formated
string_formated = json.dumps(python_data, sort_keys=True, indent=4, ensure_ascii=False)
#print(string_formated)
#### 读取data.json文件为python_data
with open('data.json', 'r', encoding='utf-8') as f:
python_data = json.load(f) # list
#### 将string_data转为python_data_2
python_data_2 = json.loads('[1, 2, 3]') # list
print(python_data_2)
网友评论