向fi打印词典列表

2024-06-25 23:30:34 发布

您现在位置:Python中文网/ 问答频道 /正文

Python 3.6版

我有一个生成词典列表的程序。你知道吗

如果我打印到屏幕上:

print(json.dumps(output_lines, indent=4, separators=(',', ': ')))

它完全按照我想看到的那样打印出来:

[
    {
        "runts": 0,     
        "giants": 0,     
        "throttles": 0,
        "input errors": 0,
        "CRC": 0,
        "frame": 0,
        "overrun": 0,
        "ignored": 0,
        "watchdog": 0,
        "pause input": 0,
        "input packets with dribble condition detected": 0,
        "underruns": 0,
        "output errors": 0,
        "collisions": 0,
        "interface resets": 2,
        "babbles": 0,
        "late collision": 0,
        "deferred": 0,
        "lost carrier": 0,
        "no carrier": 0,
        "PAUSE output": 0,
        "output buffer failures": 0,
        "output buffers swapped out": 0
    },
    {
        "runts": 0,
        "giants": 0,
        "throttles": 0,
        "input errors": 0,
        "CRC": 0,
        "frame": 0,
        "overrun": 0,
        "ignored": 0,
        "watchdog": 0,
        "pause input": 0,
        "input packets with dribble condition detected": 0,
        "underruns": 0,
        "output errors": 0,
        "collisions": 0,
        "interface resets": 2,
        "babbles": 0,
        "late collision": 0,
        "deferred": 0,
        "lost carrier": 0,
        "no carrier": 0,
        "PAUSE output": 0,
        "output buffer failures": 0,
        "output buffers swapped out": 0
    },     

但如果我想把它打印到一个文件中:

outputfile = ("d:\\mark\\python\\Projects\\error_detect\\" + hostname)
# print(json.dumps(output_lines, indent=4, separators=(',', ': ')))
output_lines.append(json.dumps(output_lines, indent=4, separators=(',', ': ')))
del output_lines[-1]
with open(outputfile, 'w') as f:
    json.dump(output_lines, f)

文件是一行巨大的文本。你知道吗

我希望文件中的格式与打印到屏幕时的格式相同。你知道吗

我不明白为什么我会丢失格式。你知道吗


Tags: 文件jsoninputoutput屏幕格式withlines
3条回答

假设你的程序生成了这个字典列表

>>> list_of_dicts = [dict(zip(list(range(2)),list(range(2)))), dict(zip(list(range(2)),list(range(2))))]
>>> list_of_dicts
[{0: 0, 1: 1}, {0: 0, 1: 1}]

你能做的就是

>>> import json
>>> str_object = json.dumps(list_of_dicts, indent=4)
>>> repr(str_object)
'[\n    {\n        "0": 0, \n        "1": 1\n    }, \n    {\n        "0": 0, \n        "1": 1\n    }\n]'
>>> str_object
[
    {
        "0": 0, 
        "1": 1
    }, 
    {
        "0": 0, 
        "1": 1
    }
]

现在你可以写str_object

>>> with open(outputfile, 'w') as f:
        f.write(str_object)

这使得文件中的格式与您在屏幕上打印时的格式相同。你知道吗

试着简单地输出格式化的json.dumps,而不是再次通过json.dump运行它。你知道吗

with open(outputfile, 'w') as f:
    f.write(output_lines)

我想你所需要的就是json.dumpindent的配合,应该可以:

outputfile = ("d:\\mark\\python\\Projects\\error_detect\\" + hostname)
# print(json.dumps(output_lines, indent=4, separators=(',', ': ')))
# output_lines.append(json.dumps(output_lines, indent=4, separators=(',', ': ')))
# del output_lines[-1]
with open(outputfile, 'w') as f:
    json.dump(output_lines, f, indent=4, separators=(',', ': '))

格式化为字符串,然后在字符串上重新运行dump对我来说没有多大意义。你知道吗

相关问题 更多 >