您的位置:首页 > 编程语言 > Python开发

Python--使用json.dumps 将 json 格式的数据写到文件里--with open as f

2013-12-05 18:49 1021 查看
#!/usr /bin/env python
# -*- coding: utf-8 -*-

"""
使用json.dumps 将 json 格式的数据写到文件里
"""

measurements = [
{'weight': 392.3, 'color': 'purple', 'temperature': 33.4},
{'weight': 34.0, 'color': 'green', 'temperature': -3.1},
]

def store(measurements):
import json
with open('measurements.json', 'w') as f:
f.write(json.dumps(measurements))

if __name__ == "__main__":
store(measurements)


执行结果:会在当前目录下生成measurements.json 文件。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Python