python之json數據存儲


# 數據存儲:json.dump()和json.load()
# date:2017-07-17
import json

file_name = 'D:/json_file.txt'
nums = [3, 4, 5, 7, 1, 9]
# nums = {"name": "Mike", "age": 12}
with open(file_name, 'w') as file_obj:
    '''寫入json文件'''
    json.dump(nums, file_obj)
    print("寫入json文件:", nums)

with open(file_name) as file_obj:
    '''讀取json文件'''
    numbers = json.load(file_obj)  # 返回列表數據,也支持字典
    print("讀取json文件:", numbers)

  

 運行結果:

寫入json文件: [3, 4, 5, 7, 1, 9]
讀取json文件: [3, 4, 5, 7, 1, 9]

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM