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,每个变量都在新行

      Python JSON dump / append to .txt with each variable on new line(Python JSON转储/附加到.txt,每个变量都在新行)

      <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,每个变量都在新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Adding config modes to Plotly.Py offline - modebar(将配置模式添加到 Plotly.Py 离线 - 模式栏)
                Plotly: How to style a plotly figure so that it doesn#39;t display gaps for missing dates?(Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙?)
                python save plotly plot to local file and insert into html(python将绘图保存到本地文件并插入到html中)
                Plotly: What color cycle does plotly express follow?(情节:情节表达遵循什么颜色循环?)
                How to save plotly express plot into a html or static image file?(如何将情节表达图保存到 html 或静态图像文件中?)
                Plotly: How to make a line plot from a pandas dataframe with a long or wide format?(Plotly:如何使用长格式或宽格式的 pandas 数据框制作线图?)

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

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

                    1. <tfoot id='UN84L'></tfoot>
                    2. <legend id='UN84L'><style id='UN84L'><dir id='UN84L'><q id='UN84L'></q></dir></style></legend>
                      • <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>