python 基础 7.8 json--下

 
一. 文件和json 之间的转换
1. json.dump()
 
#/usr/bin/python
#coding=utf-8
#@Time   :2017/11/13 0:12
#@Auther :liuzhenchuan
#@File   :json -下.py
 
##文件相关的
#load 肯定是从文件中摘出json 数据,load 肯定是把文件转换成json数据
#dump 就是爸json 数据写入到文件中。
 
# 示例1:把json 写入到文件中
import json
 
jsondate = {'a':1,'b':2.,'c':3,'d':4}
with open('a.txt','w+') as fd:
     json.dump(jsondate,fd)
 
 
#示例:把文件用json格式读出来
   with open('a.txt','r') as fd1:
     m = json.load(fd1)
     print m
     print type(m)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
原文地址:https://www.cnblogs.com/lzcys8868/p/7824778.html