【问题标题】:How to create a json file in python? [closed]如何在python中创建一个json文件? [关闭]
【发布时间】:2021-12-05 09:33:43
【问题描述】:

根据以下字段:

url ="blewpass.com"
ssl = "yes"
tags = [{'html':'hi'},{'title':'My first page'},{'h2':'My First Line'},{'p':'Hey'},{'h2':'My Second Line'}]

我想将这些字段转换为 JSON 格式并将其存储在 JSON 文件中。我无法将标签转换为单个字典,因为键不能包含许多值。那么,我想将这些字段直接转换为 JSON 吗?如何从这些字段中创建一个妥善管理的 json 数据格式?

【问题讨论】:

  • 谷歌或上面的搜索栏没有帮助?
  • 预期输出是什么?只有你想要在 JSON 中的标签?如果是,请尝试json.dumps(tags)
  • “我无法将标签转换为单个字典,因为键不能保存许多值” 这没有任何意义。您清楚地知道列表是什么,因为您已经在使用它们。这也没有意义,因为 JSON 有同样的限制

标签: python json python-3.x list


【解决方案1】:

如果只想将所有字段写入json文件,

试试

import json

url ="blewpass.com"
ssl = "yes"
tags = [{'html':'hi'},{'title':'My first page'},{'h2':'My First Line'},{'p':'Hey'},{'h2':'My Second Line'}]

temp_json = tags
temp_json.insert(0,({"url":url,"ssl":ssl}))

file = open("filename.json",'w')

json.dump(temp_json,file)

【讨论】:

  • 不仅仅是标签,所有三个字段都放到一个json文件中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-15
  • 1970-01-01
  • 2014-01-16
  • 2017-08-28
  • 2014-06-09
  • 1970-01-01
相关资源
最近更新 更多