每次用到这些函数总是很懵,所以把今天学习到的进行整理归纳,并记录。
dump:将dict类型转换为json字符串格式,写入到文件 (易存储)
a_dict = {'a':'1111','b':'2222'} json.dump(a_dict, open('demo.json', 'w')dumps:将dict转换为string (易传输)
a_dict = {'a':'1111','b':'2222'} a_str = json.dumps(a_dict)比如说有这样的一个 data={‘username’:‘李华’,‘sex’:‘male’,‘age’:16}现在用json包来处理这条json数据
import json data = {'username':'李华','sex':'male','age':16} in_json = json.dumps(data) >>>import json >>>data = {'username':'李华','sex':'male','age':16} >>>in_json = json.dumps(data) >>>in_json '{"sex": "male", "age": 16, "username": "\\u674e\\u534e"}注:这里可以注意到ensure_ascii = True时显示的为四位编码而非中文,所以要想显示中文需要将此参数改为False
接下来我们可以看一下dumps()/dump()的文档
def dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True,allow_nan=True, cls=None, indent=None, separators=None,default=None, sort_keys=False, **kw): def dump(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True,allow_nan=True, cls=None, indent=None, separators=None,default=None, sort_keys=False, **kw):其中dumps()里面的参数解释:
Serialize obj to a JSON formatted str.(字符串表示的json对象) Skipkeys:默认值是False,如果dict的keys内的数据不是python的基本类型(str,unicode,int,long,float,bool,None),设置为False时,就会报TypeError的错误。此时设置成True,则会跳过这类key ensure_ascii:,当它为True的时候,所有非ASCII码字符显示为\uXXXX序列,只需在dump时将ensure_ascii设置为False即可,此时存入json的中文即可正常显示。) If check_circular is false, then the circular reference check for container types will be skipped and a circular reference will result in an OverflowError (or worse). If allow_nan is false, then it will be a ValueError to serialize out of range float values (nan, inf, -inf) in strict compliance of the JSON specification, instead of using the JavaScript equivalents (NaN, Infinity, -Infinity). indent:可理解为缩进字符数,应该是一个非负的整型,如果是0就是顶格分行显示,如果为空就是一行最紧凑显示,否则会换行且按照indent的数值显示前面的空白分行显示,这样打印出来的json数据也叫pretty-printed json separators:分隔符,实际上是(item_separator, dict_separator)的一个元组,默认的就是(‘,’,’:’);这表示dictionary内keys之间用“,”隔开,而KEY和value之间用“:”隔开。 default(obj) is a function that should return a serializable version of obj or raise TypeError. The default simply raises TypeError. sort_keys:将数据根据keys的值进行排序。 To use a custom JSONEncoder subclass (e.g. one that overrides the .default() method to serialize additional types), specify it with the cls kwarg; otherwise JSONEncoder is used.
注:一般 indent为几就缩进几个空格一个tab那么indent = 4,ensure_ascii=False
其中dump()里面的参数解释(只有一个fp参数不一样,其他的都一样):
Serialize obj as a JSON formatted stream to fp (a.write()-supporting file-like object). 可以用dumps()函数里面的参数进行调整一下,结果就是可以输出中文。
>>>import json >>>data = {'username':'李华','sex':'male','age':16} >>>json_dic2 = json.dumps(data,sort_keys=True,indent=4,separators(',',':'),ensure_ascii=False) >>>print(json_dic2) { "username":"李华", "sex":"male", "age":16 }load:针对文件句柄,将json格式的字符转换为dict,从文件中读取 (将string转换为dict)
a_json = json.load(open('demo.json','r'))loads:针对内存对象,将string转换为dict (将string转换为dict)
a = json.loads('{'a':'1111','b':'2222'}')注: 这里我经常会用json.loads()将字符型转成数值型
>> a = '10' >> print(type(a)) >> <class 'str'> >> import json >> b = json.loads(a) >> print(type(b)) >> <class 'int'>如何将一个Python数据结构转换为JSON:
import json data = { 'name' : 'ACME', 'shares' : 100, 'price' : 542.23 } json_str = json.dumps(data)如何将一个JSON编码的字符串转换回一个Python数据结构
data = json.loads(json_str)如果你要处理的是文件而不是字符串,你可以使用 json.dump() 和 json.load() 来编码和解码JSON数据。
# Writing JSON data with open('data.json', 'w') as f: json.dump(data, f) # Reading data back with open('data.json', 'r') as f: data = json.load(f)