Python の json.dump()

Python の json.dump()

JSON の完全な形式は JavaScript Object Notation です。データの保存と転送には、プログラミング言語のテキストで構成されるスクリプト (実行可能) ファイルが使用されることを意味します。 Python は、と呼ばれる組み込みパッケージを通じて JSON をサポートします。 json> 。この機能を使用するには、Python スクリプトで json パッケージをインポートします。 JSON 内のテキストは、キーと値のマッピング内の値を含む引用符付き文字列によって行われます。 { }> 。 Pythonの辞書に似ています。

json.dump()

json> Pythonモジュールのモジュールには、と呼ばれるメソッドが用意されています。 dump()> これにより、Python オブジェクトが適切な json オブジェクトに変換されます。のわずかなバリエーションです dumps()> 方法。

dump() と dumps() の違い

ごみ() ダンプ()
dump() メソッドは、Python オブジェクトをファイルに保存する必要がある場合に使用されます。 dumps() は、オブジェクトが文字列形式である必要がある場合に使用され、解析、出力などに使用されます。
dump() には、出力を引数として保存する必要がある json ファイル名が必要です。 dumps() では、そのようなファイル名を渡す必要はありません。
このメソッドはメモリに書き込み、その後ディスクに書き込むコマンドを別途実行します。 このメソッドは json ファイルに直接書き込みます
より速い方法 2倍遅い

dump() とその引数

構文: json.dump(d、skipkeys=False、ensure_ascii=True、check_circular=True、allow_nan=True、cls=None、indent=None、separators=None)

パラメーター:

  • インデント : json ファイルの可読性が向上します。このパラメータに渡すことができる値は単に二重引用符( ''> )、任意の整数値。単純な二重引用符を使用すると、すべてのキーと値のペアが新しい行に表示されます。

    例:




    import> json> > # python object(dictionary) to be dumped> dict1> => {> > 'emp1'> : {> > 'name'> :> 'Lisa'> ,> > 'designation'> :> 'programmer'> ,> > 'age'> :> '34'> ,> > 'salary'> :> '54000'> > },> > 'emp2'> : {> > 'name'> :> 'Elis'> ,> > 'designation'> :> 'Trainee'> ,> > 'age'> :> '24'> ,> > 'salary'> :> '40000'> > },> }> > # the json file where the output must be stored> out_file> => open> (> 'myfile.json'> ,> 'w'> )> > json.dump(dict1, out_file, indent> => 6> )> > out_file.close()>

    出力:

    インデント引用符

  • スキップキー: キーが int、float、string、None、bool などの標準で許可されている型でない場合、ダンプ中にエラーが生成されます。このパラメータが次のように設定されている場合にそれを回避するには、 真実

    例:




    import> json> > # python object(dictionary) to be dumped> dict1> => {> > (> 'addresss'> ,> 'street'> ):> 'Brigade road'> ,> }> > # the json file where the output must be stored> out_file> => open> (> 'myfile.json'> ,> 'w'> )> > json.dump(dict1, out_file, indent> => 6> )> > out_file.close()>

    出力:

    Skipkeys が true に設定されていない場合、次のエラーが生成されます。

    Python json

  • 区切り文字: このパラメータは 1 つまたは 2 つの値を取ります。最初の値は、あるキーと値のペアを別のキーと値のペアから区切る記号を指定します。次のパラメータは、値とそのキーを区切る記号を指定します。
  • ソートキー: このパラメータはブール値を受け取ります。 True に設定されている場合、キーは昇順に設定されます。それ以外の場合、キーは Python オブジェクトのように表示されます。
  • ensure_ascii: このパラメータもブール値のみを受け取ります。 true に設定されていない場合、非 ASCII 文字はそのまま出力ファイルにダンプされます。デフォルトの値は次のとおりです 真実

    違いを確認するには、以下の 2 つのコードを参照してください。

    例 1:




    # dictionary to be dumped> d> => {> 'lang'> :> '??? ????'> }> > with> open> (> 'myfile.json'> ,> 'w'> , encoding> => 'utf8'> ) as json_file:> > json.dump(d, json_file, ensure_ascii> => False> )>

    出力:

    Python-json

    例 2: True に設定されている場合、json ファイルの内容は次のようになります。




    import> json> > > # dictionary to be dumped> d> => {> 'lang'> :> '??? ????'> }> > with> open> (> 'myfile.json'> ,> 'w'> , encoding> => 'utf8'> ) as json_file:> > json.dump(d, json_file, ensure_ascii> => True> )>

    出力:

    Python-json-2

  • 許可入力: これは、浮動小数点値の範囲をシリアル化するのに役立ちます。

    例 1:




    import> json> > > # dictionary to be dumped> d> => {> > 'a'> :> 1> ,> > 'x'> :> float> (> 'nan'> )> }> > with> open> (> 'myfile.json'> ,> 'w'> , encoding> => 'utf8'> ) as json_file:> > json.dump(d, json_file, allow_nan> => False> )>

    出力:

    Python-json

    例 2: True に設定すると、エラーは生成されません。 json ファイルの内容は次のようになります。




    import> json> > > # dictionary to be dumped> d> => {> > 'a'> :> 1> ,> > 'x'> :> float> (> 'nan'> )> }> > with> open> (> 'myfile.json'> ,> 'w'> , encoding> => 'utf8'> ) as json_file:> > json.dump(d, json_file, allow_nan> => True> )>

    出力:

    Python-json