【发布时间】:2012-11-23 14:41:26
【问题描述】:
假设我想使用 Json 将多个变量转储到磁盘。我通常这样做的方法是将我的变量创建为字典中的条目,然后将字典转储到磁盘:
with open(p_out, 'wb') as fp:
json.dump(my_dictionary, fp)
这将创建一个 json 文件,其中字典保存在一个长行中:
{"variable_1": something, "variable_2" something_else, ...}
我不喜欢。我希望将我的变量转储到文本文件中,每行一个变量,例如这些行:
{variable_1: something\n
variable_2: something\n
variable_3: something_else}
有没有办法在 Python 中使用 Json 来做到这一点?
【问题讨论】:
-
假设您有字典
{'variable_1': 'something'},您希望输出文件包含"variable_1": "something"还是variable_1: something。没有引号就不是 JSON。