标签:
先看代码实例
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