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


#!/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 文件。


版权声明:本文为xiaowanggedege原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。