当使用json.dump保存dict时,它只是一个单行.我想让这个像人类可读的格式,如本网站的确 - https://jsonformatter.curiousconcept.com
我怎么能在python中这样做?我试图捕获pprint输出,但它是一个无效的字符串来存储JSON.
具体来说,是否有一种可接受的默认方式直接在python中执行此操作?
我有以下数据结构:
{'row_errors': {'hello.com': {'template': [u'This field is required.']}}}
Run Code Online (Sandbox Code Playgroud)
当我在python中使用pprint时,我得到了
{'row_errors': {'hello.com': {'template': [u'This field is required.']}}}
Run Code Online (Sandbox Code Playgroud)
但是,我非常希望它像以下一样打印:
{'row_errors':
{'hello.com':
{'template': [u'This field is required.']}}}
Run Code Online (Sandbox Code Playgroud)
这可以通过pprint配置吗?(我更喜欢pprint,因为我在jinja模板中打印它).
我有一个python脚本,我想打印JSON输出看起来像这样:
{
"authMode": "open",
"enabled": false,
"ipAssignmentMode": "NAT mode",
"name": "Unconfigured SSID 14",
"number": 13,
"perClientBandwidthLimitDown": 0,
"perClientBandwidthLimitUp": 0,
"splashPage": "None",
"ssidAdminAccessible": false
},
{
"authMode": "open",
"enabled": false,
"ipAssignmentMode": "NAT mode",
"name": "Unconfigured SSID 15",
"number": 14,
"perClientBandwidthLimitDown": 0,
"perClientBandwidthLimitUp": 0,
"splashPage": "None",
"ssidAdminAccessible": false
}
Run Code Online (Sandbox Code Playgroud)
但我的输出看起来像这样:
{u'authMode': u'open',
u'enabled': False,
u'ipAssignmentMode': u'NAT mode',
u'name': u'Unconfigured SSID 14',
u'number': 13,
u'perClientBandwidthLimitDown': 0,
u'perClientBandwidthLimitUp': 0,
u'splashPage': u'None',
u'ssidAdminAccessible': False},
{u'authMode': u'open',
u'enabled': False,
u'ipAssignmentMode': u'NAT mode',
u'name': u'Unconfigured SSID 15', …Run Code Online (Sandbox Code Playgroud) 我想让 VSC 终端中的 JSON 字符串输出更漂亮。
我目前在 venv 中使用 python 3.8.5 32 位,以及最新版本的 VSC。我已经安装了 python 和 python 扩展包。我尝试过 Pretify JSON 但似乎没有做任何事情。
到目前为止我已经尝试使用:
print(json.dumps(json.loads(response.text), sort_keys=True, indent=4, separators=(",", ": ")))
然而,这只会产生一个难以读取的大字符串输出的图像
我是 Python 的相对初学者,最近几天才开始使用 VSC。
我已经使用了请求模块,现在我得到了json格式的数据,并且通过这个如何使用Python的相同帮助我写了一个JSON文件,我编写了我的代码,在执行代码时它给了我一个错误Expected a string or buffer,所以我改变了变量传递给解析器到字符串.现在它再次出现了另一个错误.
#Import
import requests
import json
r = requests.post('http://httpbin.org/post', data = {'key':'value'})
print(r.status_code)
got_data_in_json = r.json()
parsed_json = json.loads(str(got_data_in_json))
print(json.dumps(parsed_json, indent=4 ,sort_keys=True))
Run Code Online (Sandbox Code Playgroud)
错误日志:
python requests_post.py
200
Traceback (most recent call last):
File "requests_post.py", line 8, in <module>
parsed_json = json.loads(str(got_data_in_json))
File "/usr/lib/python2.7/json/__init__.py", line 339, in loads
return _default_decoder.decode(s)
File "/usr/lib/python2.7/json/decoder.py", line 364, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python2.7/json/decoder.py", line 380, in raw_decode
obj, end = self.scan_once(s, …Run Code Online (Sandbox Code Playgroud) 这是一个后续问题这一个。我需要同时对python dict进行prettyprint(这样我才能在视觉上对其进行检查/修改),并且我需要以json格式(使用双引号)输出。
pprintmodule漂亮地打印字典,但是使用单引号(不是json!)。这是链接的其他问题/答案的主题。
json.dumps 将使用双引号,但将其打印在大行中(难以阅读!)
我们如何才能做到这两者?
我正在读取JSON文件,添加字段,然后写入新的JSON文件。
我读取的JSON文件links.json如下所示:
[{"negativeNode":"osgb4000000023183407","toid":"osgb4000000023296573","term":"Private Road - Restricted Access","polyline":[492019.481,156567.076,492028,156567,492041.667,156570.536,492063.65,156578.067,492126.5,156602],"positiveNode":"osgb4000000023183409","index":1,"nature":"Single Carriageway"}
,{"negativeNode":"osgb4000000023763485","toid":"osgb4000000023296574","term":"Private Road - Restricted Access","polyline":[492144.493,156762.059,492149.35,156750,492195.75,156630],"positiveNode":"osgb4000000023183408","index":2,"nature":"Single Carriageway"}
,{"negativeNode":"osgb4000000023183650","toid":"osgb4000000023296638","term":"Private Road - Restricted Access","polyline":[492835.25,156873.5,493000,156923,493018.061,156927.938],"positiveNode":"osgb4000000023183652","index":3,"nature":"Single Carriageway"}
,{"negativeNode":"osgb4000000023181163","toid":"osgb4000000023388466","term":"Local Street","polyline":[498136.506,149148.313,498123.784,149143.969,498119.223,149143.411,498116.43,149143.318,498113.638,149145.179],"positiveNode":"osgb4000000023806248","index":4,"nature":"Single Carriageway"}
]
Run Code Online (Sandbox Code Playgroud)
我打开JSON文件,读取它,创建一个新字段,然后将其转储到新文件中:
import json
links_file = open('links.json')
links = json.load(links_file)
for link in links:
link['length'] = 10
with open('links_new.json','w') as outfile:
json.dump(links, outfile)
Run Code Online (Sandbox Code Playgroud)
这样成功导出,我可以使用文本编辑器(Sublime Text)进行检查
[{"index": 1, "term": "Private Road - Restricted Access", "nature": "Single Carriageway", "negativeNode": "osgb4000000023183407", "toid": "osgb4000000023296573", "length": 10, "polyline": [492019.481, 156567.076, 492028, 156567, 492041.667, 156570.536, 492063.65, 156578.067, 492126.5, 156602], "positiveNode": "osgb4000000023183409"}, …Run Code Online (Sandbox Code Playgroud)