在将itens附加到json时使用“rstrip”

青葱年少 python 216

原文标题using “rstrip” while appending itens to a json

我正在尝试将 URL 附加到稍后将转换为 json 的列表中。

这些 URL 具有相同的域,并且我知道它们只会在此类 URL 中公开版本后才会更改,例如:

“https://www.example.com/v1/item/anotheritem/otheritem”

所以,我需要的只是我的 json 中 URL 的这一部分:“v1/item/anotheritem/otheritem”

我的代码目前是这样的:

participants=[]

for institution in data[0:1]:
    for auth in institution["auth"]:
        for api in auth["api"]:
            myList.append({
                'ID':[institution["ID"]],
                'Name':[institution["Name"]],
                'ParentInstitution':[institution["ParentInstitution"]],
                'ParentId':[institution["ParentId"]],
                'Roles':[A["Role"] for A in institution["InstitutionRoles"]],
                'BrandId':[auth["BrandId"]],
                'ApiFamily':[api["ApiFamily"]],
                'ApiEndpoints':[A["ApiEndpoint"] for A in api["ApiEndpoints"]]

我稍后会将此数据提取转换为 json 并用于其他目的。

现在,如果我的 URL 更小,这将非常有帮助,因此我需要剥离它。

我相信我可以这样做:

'ApiEndpoints':[A["ApiEndpoint"].rstrip('/v1') for A in api["ApiEndpoints"]]

但这对 URL 没有实际效果。

我知道要让它工作,我必须使用这样的东西:

Stripped = ApiEndpoint.rstrip('/v1') (...)

但由于我是 python 新手,我不太确定如何在我附加的列表中执行此操作。

你能帮帮我吗?

原文链接:https://stackoverflow.com//questions/71454150/using-rstrip-while-appending-itens-to-a-json

回复

我来回复
  • 暂无回复内容