欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python json格式化打印實(shí)現(xiàn)過(guò)程解析

 更新時(shí)間:2020年07月21日 09:17:22   作者:捷后愚生  
這篇文章主要介紹了Python json格式化打印實(shí)現(xiàn)過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

編寫python腳本,調(diào)試的時(shí)候需要打印json格式報(bào)文,直接打印看不出層次,可以使用json.dumps格式化打印

import json
import requests

def test_json():
  r=requests.get('https://home.testing-studio.com/categories.json')
  print(r.json())
  print(json.dumps(r.json(), indent=2,ensure_ascii=False)) # r.json()是json對(duì)象,indent表示縮進(jìn),ensure_ascii設(shè)置編碼
格式化打印前:

格式化打印前:

格式化打印后:

json.dumps方法源碼:

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):
  """Serialize ``obj`` to a JSON formatted ``str``.

  If ``skipkeys`` is true then ``dict`` keys that are not basic types
  (``str``, ``int``, ``float``, ``bool``, ``None``) will be skipped
  instead of raising a ``TypeError``.

  If ``ensure_ascii`` is false, then the return value can contain non-ASCII
  characters if they appear in strings contained in ``obj``. Otherwise, all
  such characters are escaped in JSON strings.

  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``).

  If ``indent`` is a non-negative integer, then JSON array elements and
  object members will be pretty-printed with that indent level. An indent
  level of 0 will only insert newlines. ``None`` is the most compact
  representation.

  If specified, ``separators`` should be an ``(item_separator, key_separator)``
  tuple. The default is ``(', ', ': ')`` if *indent* is ``None`` and
  ``(',', ': ')`` otherwise. To get the most compact JSON representation,
  you should specify ``(',', ':')`` to eliminate whitespace.

  ``default(obj)`` is a function that should return a serializable version
  of obj or raise TypeError. The default simply raises TypeError.

  If *sort_keys* is true (default: ``False``), then the output of
  dictionaries will be sorted by key.

  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.

  """
  # cached encoder
  if (not skipkeys and ensure_ascii and
    check_circular and allow_nan and
    cls is None and indent is None and separators is None and
    default is None and not sort_keys and not kw):
    return _default_encoder.encode(obj)
  if cls is None:
    cls = JSONEncoder
  return cls(
    skipkeys=skipkeys, ensure_ascii=ensure_ascii,
    check_circular=check_circular, allow_nan=allow_nan, indent=indent,
    separators=separators, default=default, sort_keys=sort_keys,
    **kw).encode(obj)

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • PyCharm中如何切換Python版本

    PyCharm中如何切換Python版本

    這篇文章主要介紹了PyCharm中如何切換Python版本問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • Python數(shù)據(jù)可視化實(shí)踐之使用Matplotlib繪制圖表

    Python數(shù)據(jù)可視化實(shí)踐之使用Matplotlib繪制圖表

    數(shù)據(jù)可視化是數(shù)據(jù)分析的重要環(huán)節(jié),通過(guò)將數(shù)據(jù)轉(zhuǎn)化為圖形,可以更直觀地展示數(shù)據(jù)特征和規(guī)律。Python中的Matplotlib庫(kù)是一個(gè)強(qiáng)大的數(shù)據(jù)可視化工具,本文將帶您了解Matplotlib的基本使用方法,以及如何繪制常見(jiàn)的圖表
    2023-05-05
  • 深入了解Python中Pytest Markers的使用方法

    深入了解Python中Pytest Markers的使用方法

    從這篇開(kāi)始,逐一解決fixture是啥,mark是啥,參數(shù)request是啥,鉤子函數(shù)是啥,parametrize參數(shù)化是啥,這些問(wèn)題,本片先介紹一下mark是啥,以及如何使用
    2023-09-09
  • 舉例講解Python裝飾器

    舉例講解Python裝飾器

    這篇文章主要介紹了Python裝飾器的相關(guān)資料,幫助大家更好的理解和使用python,感興趣的朋友可以了解下
    2020-12-12
  • Python中Pandas庫(kù)的數(shù)據(jù)處理與分析

    Python中Pandas庫(kù)的數(shù)據(jù)處理與分析

    Python的Pandas庫(kù)是數(shù)據(jù)科學(xué)領(lǐng)域中非常重要的一個(gè)庫(kù),它使數(shù)據(jù)清洗和分析工作變得更快更簡(jiǎn)單,Pandas結(jié)合了NumPy的高性能數(shù)組計(jì)算功能以及電子表格和關(guān)系型數(shù)據(jù)庫(kù)(如SQL)的靈活數(shù)據(jù)處理能力,需要的朋友可以參考下
    2023-07-07
  • Python實(shí)現(xiàn)遺傳算法(二進(jìn)制編碼)求函數(shù)最優(yōu)值方式

    Python實(shí)現(xiàn)遺傳算法(二進(jìn)制編碼)求函數(shù)最優(yōu)值方式

    今天小編就為大家分享一篇Python實(shí)現(xiàn)遺傳算法(二進(jìn)制編碼)求函數(shù)最優(yōu)值方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-02-02
  • python生成器表達(dá)式和列表解析

    python生成器表達(dá)式和列表解析

    最近在學(xué)習(xí)python的過(guò)程中,對(duì)列表解析及生成器表達(dá)式有點(diǎn)疑惑。從表面上看,兩者很相似,只有一點(diǎn)不同:列表解析采用中括號(hào)[]來(lái)包含,生成器表達(dá)式采用小括號(hào)()來(lái)包含。本文我們就來(lái)詳細(xì)看下python生成器表達(dá)式和列表解析
    2016-03-03
  • python數(shù)據(jù)結(jié)構(gòu)鏈表之單向鏈表(實(shí)例講解)

    python數(shù)據(jù)結(jié)構(gòu)鏈表之單向鏈表(實(shí)例講解)

    下面小編就為大家?guī)?lái)一篇python數(shù)據(jù)結(jié)構(gòu)鏈表之單向鏈表(實(shí)例講解)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-07-07
  • 深度學(xué)習(xí)環(huán)境配置之Anaconda安裝和pip源方式

    深度學(xué)習(xí)環(huán)境配置之Anaconda安裝和pip源方式

    這篇文章主要介紹了深度學(xué)習(xí)環(huán)境配置之Anaconda安裝和pip源方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-02-02
  • python密碼學(xué)簡(jiǎn)單替代密碼解密及測(cè)試教程

    python密碼學(xué)簡(jiǎn)單替代密碼解密及測(cè)試教程

    這篇文章主要介紹了python密碼學(xué)簡(jiǎn)單替代密碼解密及測(cè)試教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05

最新評(píng)論