python读写json和txt
生活随笔
收集整理的這篇文章主要介紹了
python读写json和txt
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
讀寫json
#數據保存如json文件 import json jsObj = json.dumps(code_sec) fileObject = open('jsonFile.json', 'w') fileObject.write(jsObj) fileObject.close() #讀取json文件 # 將類文件對象中的JSON字符串直接轉換成 Python 字典 with open('jsonFile.json', 'r', encoding='utf-8') as f:ret_dic = json.load(f)print(type(ret_dic)) # 結果 <class 'dict'>print(ret_dic) # 結果 pengjunlee ret_dic讀寫txt
a = [1,3,4,5,6,7,8,9] with open('a.txt', 'w') as f: json.dump(a, f) with open('a.txt', 'r') as f: a = json.load(f) print(a)總結
以上是生活随笔為你收集整理的python读写json和txt的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: docker的安装与安装mysql(ma
- 下一篇: python 的作用