码迷,mamicode.com
首页 > 编程语言 > 详细

python json.dumps() json.dump()的区别

时间:2016-04-20 13:30:51      阅读:340      评论:0      收藏:0      [点我收藏+]

标签:

先看代码实例

In [1]: a = {‘a‘: ‘abc‘, ‘b‘: "bcd"}

In [2]: 

In [2]: import json

In [3]: print json.dumps(a)
{"a": "abc", "b": "bcd"}

In [4]: print json.dump(a)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-4-68d925ef8846> in <module>()
----> 1 print json.dump(a)

TypeError: dump() takes at least 2 arguments (1 given)

  

dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, encoding=‘utf-8‘, default=None, sort_keys=False, **kw)
Serialize ``obj`` to a JSON formatted ``str``.

 

dump(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, encoding=‘utf-8‘, default=None, sort_keys=False, **kw)
Serialize ``obj`` as a JSON formatted stream to ``fp`` (a
``.write()``-supporting file-like object).

 

简单说就是dump给的是一个类似于文件指针的东西(并不是真的指针),可以与文件操作结合,而dumps直接给的是str

实际中dump很少用。 

python json.dumps() json.dump()的区别

标签:

原文地址:http://www.cnblogs.com/wswang/p/5411826.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!