需要有一个带有列表的 for 循环,以用作 get 请求中的参数

发布于 2025-01-09 07:59:08 字数 4427 浏览 0 评论 0原文

我有以下项目列表,其中每个项目都有一个 ID,

 List_Item = [['5f5e5b5a-1e2d-4c6a-bdf0-ec2bc233ab0c:471b2fd7-d1f6-4fdd-a701-8d2426e2c67b'],
     ['5f5e5b5a-1e2d-4c6a-bdf0-ec2bc233ab0c:fe43ed8e-1254-4bf3-a6d1-be4568861ce7'],
     ['5f5e5b5a-1e2d-4c6a-bdf0-ec2bc233ab0c:4ac881c8-942d-4bf2-8728-61ee8db55e74'],
     ['5f5e5b5a-1e2d-4c6a-bdf0-ec2bc233ab0c:24fa0bf7-af38-46aa-99c7-5d74a66e4132'],
     ['5f5e5b5a-1e2d-4c6a-bdf0-ec2bc233ab0c:fd9f6f71-9e39-4777-8d99-80f3b0d9e314'],
     ['5f5e5b5a-1e2d-4c6a-bdf0-ec2bc233ab0c:7fee75ec-5ed7-4542-b62f-adb6879d7d35'],
     ['5f5e5b5a-1e2d-4c6a-bdf0-ec2bc233ab0c:51e5e086-91bd-411f-9987-46cb7376fd76'],
     ['5f5e5b5a-1e2d-4c6a-bdf0-ec2bc233ab0c:8a664472-45fb-45ce-9965-2087638635f6'],
     ['5f5e5b5a-1e2d-4c6a-bdf0-ec2bc233ab0c:e4d01008-4d15-4d64-825a-d17594a00d82'],
     ['5f5e5b5a-1e2d-4c6a-bdf0-ec2bc233ab0c:b76272c1-a295-4b0d-b67c-a6345ac066c3'],
     ['5f5e5b5a-1e2d-4c6a-bdf0-ec2bc233ab0c:7a767269-d0d9-4ce6-a655-355173778141'],
     ['5f5e5b5a-1e2d-4c6a-bdf0-ec2bc233ab0c:5f93f6d9-c0ce-4006-84bf-217cdab64fce'],
     ['5f5e5b5a-1e2d-4c6a-bdf0-ec2bc233ab0c:8d062331-77b9-4c5e-87d6-2e02adaf672c'],
     ['5f5e5b5a-1e2d-4c6a-bdf0-ec2bc233ab0c:fb63787e-f5a5-4d4b-b637-99d8354fc482'],
     ['5f5e5b5a-1e2d-4c6a-bdf0-ec2bc233ab0c:dc51d14f-cfcd-4688-a8f0-fea6129d77d1'],
     ['5f5e5b5a-1e2d-4c6a-bdf0-ec2bc233ab0c:f65ece95-6cb8-4ef9-99b3-28067af26b10'],
     ['5f5e5b5a-1e2d-4c6a-bdf0-ec2bc233ab0c:74588a7a-549f-4e07-a018-557fd8e9749c']
]

我想在下面的脚本中使用此 id 列表作为参数。我想这样做的原因是因为为了访问与DataProcessingITem相关的信息,我需要给出DataProcessing的id,以便代码了解它需要搜索哪些信息并返回信息。

这里的目的是使用 id 作为参数,以便获取此研究信息。棘手的是我需要对已存储在列表中的几个 id 执行此操作。

有人知道如何实现这一目标吗?

for categoryItem in structureDictItem:
    for endpointItem in structureDictItem[categoryItem]:
        
    endpointFilenameItem = endpointItem
    
    if categoryItem in ["types"]:
        endpointFilenameItem = endpointItem.replace("/", "_")


    url = DataGalaxy_url + endpointItem

    params = {"versionId":Workspace_id,
             "includeAccessData":"true", 
              "includeAttributes":"true",
              "includeLinks":"true",
              "limit":5000
             }

    jsonResponse = requests.get(url, params=params, headers={"Authorization":accessToken}).json()

    writeContentToFile('a', customername, workspacename, categoryItem, endpointFilenameItem, jsonResponse)
    
    try:
        jsonResponsePages = jsonResponse['pages']

        if int(jsonResponsePages) != 1:
            for i in range(2, jsonResponsePages+1, 1):
                for x in range(0,30):

                    params["page"] = str(i)
                    params["dataProcessingId"] = List_Item[x]

                jsonResponse = requests.get(url=url, params = params, headers={"Authorization":accessToken}).json()['results']

                writeContentToFile('a', customername, workspacename, categoryItem, endpointFilenameItem, jsonResponse)
                
    except:
        print(endpointItem)
        next

我尝试了以下脚本,但它告诉我 dataProcessingId 必须是一个字符串

for categoryItem in structureDictItem:
for endpointItem in structureDictItem[categoryItem]:
    
    endpointFilenameItem = endpointItem
    
    if categoryItem in ["types"]:
        endpointFilenameItem = endpointItem.replace("/", "_")


    url = DataGalaxy_url + endpointItem

    params = {"versionId":Workspace_id,
             "includeAccessData":"true", 
              "includeAttributes":"true",
              "includeLinks":"true",
              "limit":5000
             }

    jsonResponse = requests.get(url, params=params, headers={"Authorization":accessToken}).json()

    writeContentToFile('a', customername, workspacename, categoryItem, endpointFilenameItem, jsonResponse)
    
    try:
        jsonResponsePages = jsonResponse['pages']

        if int(jsonResponsePages) != 1:
            for i in range(2, jsonResponsePages+1, 1):
                for x in range(0,30):

                    params["page"] = str(i)
                    params["dataProcessingId"] = List_Item[x]

                jsonResponse = requests.get(url=url, params = params, headers={"Authorization":accessToken}).json()['results']

                writeContentToFile('a', customername, workspacename, categoryItem, endpointFilenameItem, jsonResponse)
                
    except:
        print(endpointItem)
        next

I have the following List of Item with for each of them an ID

 List_Item = [['5f5e5b5a-1e2d-4c6a-bdf0-ec2bc233ab0c:471b2fd7-d1f6-4fdd-a701-8d2426e2c67b'],
     ['5f5e5b5a-1e2d-4c6a-bdf0-ec2bc233ab0c:fe43ed8e-1254-4bf3-a6d1-be4568861ce7'],
     ['5f5e5b5a-1e2d-4c6a-bdf0-ec2bc233ab0c:4ac881c8-942d-4bf2-8728-61ee8db55e74'],
     ['5f5e5b5a-1e2d-4c6a-bdf0-ec2bc233ab0c:24fa0bf7-af38-46aa-99c7-5d74a66e4132'],
     ['5f5e5b5a-1e2d-4c6a-bdf0-ec2bc233ab0c:fd9f6f71-9e39-4777-8d99-80f3b0d9e314'],
     ['5f5e5b5a-1e2d-4c6a-bdf0-ec2bc233ab0c:7fee75ec-5ed7-4542-b62f-adb6879d7d35'],
     ['5f5e5b5a-1e2d-4c6a-bdf0-ec2bc233ab0c:51e5e086-91bd-411f-9987-46cb7376fd76'],
     ['5f5e5b5a-1e2d-4c6a-bdf0-ec2bc233ab0c:8a664472-45fb-45ce-9965-2087638635f6'],
     ['5f5e5b5a-1e2d-4c6a-bdf0-ec2bc233ab0c:e4d01008-4d15-4d64-825a-d17594a00d82'],
     ['5f5e5b5a-1e2d-4c6a-bdf0-ec2bc233ab0c:b76272c1-a295-4b0d-b67c-a6345ac066c3'],
     ['5f5e5b5a-1e2d-4c6a-bdf0-ec2bc233ab0c:7a767269-d0d9-4ce6-a655-355173778141'],
     ['5f5e5b5a-1e2d-4c6a-bdf0-ec2bc233ab0c:5f93f6d9-c0ce-4006-84bf-217cdab64fce'],
     ['5f5e5b5a-1e2d-4c6a-bdf0-ec2bc233ab0c:8d062331-77b9-4c5e-87d6-2e02adaf672c'],
     ['5f5e5b5a-1e2d-4c6a-bdf0-ec2bc233ab0c:fb63787e-f5a5-4d4b-b637-99d8354fc482'],
     ['5f5e5b5a-1e2d-4c6a-bdf0-ec2bc233ab0c:dc51d14f-cfcd-4688-a8f0-fea6129d77d1'],
     ['5f5e5b5a-1e2d-4c6a-bdf0-ec2bc233ab0c:f65ece95-6cb8-4ef9-99b3-28067af26b10'],
     ['5f5e5b5a-1e2d-4c6a-bdf0-ec2bc233ab0c:74588a7a-549f-4e07-a018-557fd8e9749c']
]

I would like to use this list of id in my script below as a parameter. The reason I want to do this is because in order to have access to information related to DataProcessingITem, I need to give the id of DataProcessing in order for the code to understand which information it needs to search for and brings back the information.

The purpose here is to use the id as a parameter in order to get this researched information. The tricky thing is that I need to do it for several id that have been stored in a list.

Someone has an idea how to achieve that?

for categoryItem in structureDictItem:
    for endpointItem in structureDictItem[categoryItem]:
        
    endpointFilenameItem = endpointItem
    
    if categoryItem in ["types"]:
        endpointFilenameItem = endpointItem.replace("/", "_")


    url = DataGalaxy_url + endpointItem

    params = {"versionId":Workspace_id,
             "includeAccessData":"true", 
              "includeAttributes":"true",
              "includeLinks":"true",
              "limit":5000
             }

    jsonResponse = requests.get(url, params=params, headers={"Authorization":accessToken}).json()

    writeContentToFile('a', customername, workspacename, categoryItem, endpointFilenameItem, jsonResponse)
    
    try:
        jsonResponsePages = jsonResponse['pages']

        if int(jsonResponsePages) != 1:
            for i in range(2, jsonResponsePages+1, 1):
                for x in range(0,30):

                    params["page"] = str(i)
                    params["dataProcessingId"] = List_Item[x]

                jsonResponse = requests.get(url=url, params = params, headers={"Authorization":accessToken}).json()['results']

                writeContentToFile('a', customername, workspacename, categoryItem, endpointFilenameItem, jsonResponse)
                
    except:
        print(endpointItem)
        next

I have tried something witht the following script but it told me that dataProcessingId must be a string

for categoryItem in structureDictItem:
for endpointItem in structureDictItem[categoryItem]:
    
    endpointFilenameItem = endpointItem
    
    if categoryItem in ["types"]:
        endpointFilenameItem = endpointItem.replace("/", "_")


    url = DataGalaxy_url + endpointItem

    params = {"versionId":Workspace_id,
             "includeAccessData":"true", 
              "includeAttributes":"true",
              "includeLinks":"true",
              "limit":5000
             }

    jsonResponse = requests.get(url, params=params, headers={"Authorization":accessToken}).json()

    writeContentToFile('a', customername, workspacename, categoryItem, endpointFilenameItem, jsonResponse)
    
    try:
        jsonResponsePages = jsonResponse['pages']

        if int(jsonResponsePages) != 1:
            for i in range(2, jsonResponsePages+1, 1):
                for x in range(0,30):

                    params["page"] = str(i)
                    params["dataProcessingId"] = List_Item[x]

                jsonResponse = requests.get(url=url, params = params, headers={"Authorization":accessToken}).json()['results']

                writeContentToFile('a', customername, workspacename, categoryItem, endpointFilenameItem, jsonResponse)
                
    except:
        print(endpointItem)
        next

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

阿楠 2025-01-16 07:59:08

List_Item 是一个列表列表,因此当您执行 List_Item[int] 时,它会返回另一个列表

List_Item[0]
['5f5e5b5a-1e2d-4c6a-bdf0-ec2bc233ab0c:471b2fd7-d1f6-4fdd-a701-8d2426e2c67b']

您需要获取列表内的字符串:

List_Item[0][0]
'5f5e5b5a-1e2d-4c6a-bdf0-ec2bc233ab0c:471b2fd7-d1f6-4fdd-a701-8d2426e2c67b'

此外,而不是使用范围函数循环列表,您可以像这样循环列表:

for item in List_Item:
    params["page"] = str(i)
    params["dataProcessingId"] = item[0]

List_Item is a list of lists, so when you do List_Item[int], it is returning another list

List_Item[0]
['5f5e5b5a-1e2d-4c6a-bdf0-ec2bc233ab0c:471b2fd7-d1f6-4fdd-a701-8d2426e2c67b']

You need to get the string inside the list:

List_Item[0][0]
'5f5e5b5a-1e2d-4c6a-bdf0-ec2bc233ab0c:471b2fd7-d1f6-4fdd-a701-8d2426e2c67b'

In addition, instead of using a range function to loop over the list, you can just loop over the list like:

for item in List_Item:
    params["page"] = str(i)
    params["dataProcessingId"] = item[0]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文