字典与文本文档的转换
生活随笔
收集整理的這篇文章主要介紹了
字典与文本文档的转换
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.將字典寫入文本文檔
dic = { 'andy':{ 'age': 23, 'city': 'beijing', 'skill': 'python' }, 'william': { 'age': 25, 'city': 'shanghai', 'skill': 'js' } } fw = open("test.txt",'w+') fw.write(str(dic)) #把字典轉化為str fw.close() import json dic = {'andy':{'age': 23,'city': 'beijing','skill': 'python'},'william': {'age': 25,'city': 'shanghai','skill': 'js'} }js = json.dumps(dic) file = open('test.txt', 'w') file.write(js) file.close()2.文本文檔到字典
fr = open("test.txt",'r+') dic = eval(fr.read()) #讀取的str轉換為字典 print(dic) fr.close() import json file = open('test.txt', 'r') js = file.read() dic = json.loads(js) print(dic) file.close()?
轉載于:https://www.cnblogs.com/joker-hk/p/9661908.html
總結
以上是生活随笔為你收集整理的字典与文本文档的转换的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java 7 中 NIO.2 的使用——
- 下一篇: iframe 子父窗口互掉 js