有没有一种方法可以使用Python提取json文件的结构细节?
我有一个像这样的文件:
{
"help": "https://?name=package_search",
"success": true,
"result": {
"count": 47,
"facets": {},
"results": [
{
"author": "ggm",
"author_email": "ggm@____.nl",
"creator_user_id": "x_x_x_x" }]}}而我只想看到这样的结构:
{
{
[
{
}
{
}
]
}
}这能用Python实现吗?
发布于 2022-02-21 13:09:46
您可以使用regex (假设数据仍然是字符串形式,还没有解析到dict):
import re
s = """{
"help": "https://?name=package_search",
"success": true,
"result": {
"count": 47,
"facets": {},
"results": [
{
"author": "ggm",
"author_email": "ggm@____.nl",
"creator_user_id": "x_x_x_x" }]}}"""
print(re.sub(r"[^{}[\]\n]", ' ', s))将给予:
{
{
{}
[
{
}]}}它使用re.sub,这是replace的regex版本,基本上是这样的:“用空格替换除括号和新行之外的所有”(参见演示)。
https://stackoverflow.com/questions/71206829
复制相似问题