1. <small id='ekC7X'></small><noframes id='ekC7X'>

  2. <legend id='ekC7X'><style id='ekC7X'><dir id='ekC7X'><q id='ekC7X'></q></dir></style></legend>
    <i id='ekC7X'><tr id='ekC7X'><dt id='ekC7X'><q id='ekC7X'><span id='ekC7X'><b id='ekC7X'><form id='ekC7X'><ins id='ekC7X'></ins><ul id='ekC7X'></ul><sub id='ekC7X'></sub></form><legend id='ekC7X'></legend><bdo id='ekC7X'><pre id='ekC7X'><center id='ekC7X'></center></pre></bdo></b><th id='ekC7X'></th></span></q></dt></tr></i><div id='ekC7X'><tfoot id='ekC7X'></tfoot><dl id='ekC7X'><fieldset id='ekC7X'></fieldset></dl></div>

    1. <tfoot id='ekC7X'></tfoot>
        <bdo id='ekC7X'></bdo><ul id='ekC7X'></ul>

      Python JSON转储/附加到.txt,每个变量都在新行

      时间:2023-09-27

      <i id='jx1DA'><tr id='jx1DA'><dt id='jx1DA'><q id='jx1DA'><span id='jx1DA'><b id='jx1DA'><form id='jx1DA'><ins id='jx1DA'></ins><ul id='jx1DA'></ul><sub id='jx1DA'></sub></form><legend id='jx1DA'></legend><bdo id='jx1DA'><pre id='jx1DA'><center id='jx1DA'></center></pre></bdo></b><th id='jx1DA'></th></span></q></dt></tr></i><div id='jx1DA'><tfoot id='jx1DA'></tfoot><dl id='jx1DA'><fieldset id='jx1DA'></fieldset></dl></div>
        <tfoot id='jx1DA'></tfoot>
            <bdo id='jx1DA'></bdo><ul id='jx1DA'></ul>

              <small id='jx1DA'></small><noframes id='jx1DA'>

                <tbody id='jx1DA'></tbody>

                <legend id='jx1DA'><style id='jx1DA'><dir id='jx1DA'><q id='jx1DA'></q></dir></style></legend>
              • 本文介绍了Python JSON转储/附加到.txt,每个变量都在新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                My code creates a dictionary, which is then stored in a variable. I want to write each dictionary to a JSON file, but I want each dictionary to be on a new line.

                My dictionary:

                hostDict = {"key1": "val1", "key2": "val2", "key3": {"sub_key1": "sub_val2", "sub_key2": "sub_val2", "sub_key3": "sub_val3"}, "key4": "val4"}
                

                Part of my code:

                g = open('data.txt', 'a')
                with g as outfile:
                  json.dump(hostDict, outfile)
                

                This appends each dictionary to 'data.txt' but it does so inline. I want each dictionary entry to be on new line. Any advice would be appreciated.

                解决方案

                Your question is a little unclear. If you're generating hostDict in a loop:

                with open('data.txt', 'a') as outfile:
                    for hostDict in ....:
                        json.dump(hostDict, outfile)
                        outfile.write('
                ')
                

                If you mean you want each variable within hostDict to be on a new line:

                with open('data.txt', 'a') as outfile:
                    json.dump(hostDict, outfile, indent=2)
                

                When the indent keyword argument is set it automatically adds newlines.

                这篇关于Python JSON转储/附加到.txt,每个变量都在新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:追加到列表列表中的一个列表也追加到所有其他列表 下一篇:如果新文件不存在则写入新文件,如果存在则追加到文件

                相关文章

              • <small id='UN84L'></small><noframes id='UN84L'>

                    <bdo id='UN84L'></bdo><ul id='UN84L'></ul>

                  1. <tfoot id='UN84L'></tfoot>
                  2. <legend id='UN84L'><style id='UN84L'><dir id='UN84L'><q id='UN84L'></q></dir></style></legend>
                  3. <i id='UN84L'><tr id='UN84L'><dt id='UN84L'><q id='UN84L'><span id='UN84L'><b id='UN84L'><form id='UN84L'><ins id='UN84L'></ins><ul id='UN84L'></ul><sub id='UN84L'></sub></form><legend id='UN84L'></legend><bdo id='UN84L'><pre id='UN84L'><center id='UN84L'></center></pre></bdo></b><th id='UN84L'></th></span></q></dt></tr></i><div id='UN84L'><tfoot id='UN84L'></tfoot><dl id='UN84L'><fieldset id='UN84L'></fieldset></dl></div>