python读写json和txt
生活随笔
收集整理的這篇文章主要介紹了
python读写json和txt
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
讀寫(xiě)json
#數(shù)據(jù)保存如json文件 import json jsObj = json.dumps(code_sec) fileObject = open('jsonFile.json', 'w') fileObject.write(jsObj) fileObject.close() #讀取json文件 # 將類(lèi)文件對(duì)象中的JSON字符串直接轉(zhuǎn)換成 Python 字典 with open('jsonFile.json', 'r', encoding='utf-8') as f:ret_dic = json.load(f)print(type(ret_dic)) # 結(jié)果 <class 'dict'>print(ret_dic) # 結(jié)果 pengjunlee ret_dic讀寫(xiě)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)總結(jié)
以上是生活随笔為你收集整理的python读写json和txt的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: docker的安装与安装mysql(ma
- 下一篇: python 的作用