Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中从RESTAPI输出有效打印_Python_Json_Python 3.x_Dictionary_Restapi - Fatal编程技术网

在Python中从RESTAPI输出有效打印

在Python中从RESTAPI输出有效打印,python,json,python-3.x,dictionary,restapi,Python,Json,Python 3.x,Dictionary,Restapi,我正在对一些软件运行一些简单的GET请求来提取和总结数据。我已经能够从RESTAPI输出中提取值,并创建了一个基本for循环来总结输出。但是,我需要更好地打印它,以便最终用户更清楚。希望下面的描述和示例能够向您展示我试图实现的目标 user_response = user.json() print(json.dumps(user_response, indent=2)) 剩余输出 user_response = { "total": 14, "offset

我正在对一些软件运行一些简单的GET请求来提取和总结数据。我已经能够从RESTAPI输出中提取值,并创建了一个基本for循环来总结输出。但是,我需要更好地打印它,以便最终用户更清楚。希望下面的描述和示例能够向您展示我试图实现的目标

user_response = user.json()
print(json.dumps(user_response, indent=2))
剩余输出

user_response = {
  "total": 14,
  "offset": 0,
  "limit": 25,
  "list": [
    {
      "id": 1321,
      "url": "/v1/domains/1001/usersets",
      "name": "test1",
      "description": "test rest 1",
      "users": [
        {
          "uId": 1033,
          "uName": "harry",
          "gId": 1034,
          "gName": "harry"
        },
        {
          "uId": 1000,
          "uName": "luke",
          "gId": 1000,
          "gName": "luke"
        }
      ]
    },
    {
      "id": 1257,
      "url": "/v1/domains/1001/usersets",
      "name": "test2",
      "description": "test rest 2",
      "users": [
        {
          "uName": "max"
        },
        {
          "uName": "joe"
        },
        {
          "uName": "polly"
        },
        {
          "uName": "tara"
        }
      ]
    },
我最终能够提取名称、描述和用户数据,但对于使用下面代码的普通最终用户来说,输出不是最容易读取的

user_response = user_response['list']
    for x in user_response:
        print("Name: ", x['name'],"\n", "Description: ", x['description'])
        x = x['users']
        print("Users: ", "\n", x, "\n")
终端中的输出

Name:  test1 
 Description:  test rest 1
Users:  
 [{'uId': 1033, 'uName': 'harry', 'gId': 1034, 'gName': 'harry'}, {'uId': 1000, 'uName': 'luke', 'gId': 1000, 'gName': 'luke'}] 

Name:  test2
 Description:  test rest 1
Users:  
 [{'uName': 'max'}, {'uName': 'joe'}, {'uName': 'polly'}, {'uName': 'tara'}] 
我希望为列表中的每一本词典打印一行新词,以便更好地阅读,但是我找不到任何关于如何做到这一点的信息,我的代码编辑也没有定论

Name:  test1 
Description:  test rest 1
Users:  
{'uId': 1033, 'uName': 'harry', 'gId': 1034, 'gName': 'harry'}, 
{'uId': 1000, 'uName': 'luke', 'gId': 1000, 'gName': 'luke'}

Name:  test2
Description:  test rest 2
Users:  
{'uName': 'max'}, 
{'uName': 'joe'},
{'uName': 'polly'},
{'uName': 'tara'}
关于如何在我当前的for循环中实现这一点,有什么想法吗


此外,如果您有任何关于更好地打印此数据的提示,我们将不胜感激。

您可以使用此示例格式化和打印输出:

for i in user_response['list']:
    print('Name: {}'.format(i['name']))
    print('Description: {}'.format(i['description']))
    print('Users:')
    print(',\n'.join(str(u) for u in i['users']))
    print()
印刷品:

Name: test1
Description: test rest 1
Users:
{'uId': 1033, 'uName': 'harry', 'gId': 1034, 'gName': 'harry'},
{'uId': 1000, 'uName': 'luke', 'gId': 1000, 'gName': 'luke'}

Name: test2
Description: test rest 2
Users:
{'uName': 'max'},
{'uName': 'joe'},
{'uName': 'polly'},
{'uName': 'tara'}

您可以使用此示例格式化和打印输出:

for i in user_response['list']:
    print('Name: {}'.format(i['name']))
    print('Description: {}'.format(i['description']))
    print('Users:')
    print(',\n'.join(str(u) for u in i['users']))
    print()
印刷品:

Name: test1
Description: test rest 1
Users:
{'uId': 1033, 'uName': 'harry', 'gId': 1034, 'gName': 'harry'},
{'uId': 1000, 'uName': 'luke', 'gId': 1000, 'gName': 'luke'}

Name: test2
Description: test rest 2
Users:
{'uName': 'max'},
{'uName': 'joe'},
{'uName': 'polly'},
{'uName': 'tara'}

你可以试试这个。它仍然会打印方括号,但会以其他方式提供您想要的输出。以表格格式打印数据可能更好:我也不知道,谢谢-需要探索的东西您可以尝试。它仍然会打印方括号,但会以其他方式提供您想要的输出。以表格格式打印数据可能更好:我也不知道,谢谢-需要探索的东西