'using "rstrip" while appending itens to a json

I'm trying to append URLs to a list that will be converted into a json later.

Those URLs have the same domain, and I know that they will only change after the version is exposed in such URL, for example:

"https://www.example.com/v1/item/anotheritem/otheritem"

So, all I need is this portion of the URL in my json: "v1/item/anotheritem/otheritem"

My code currently is like this:

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"]]

I'm later converting this data extraction in a json and using for other purposes.

Now it would be very helpful if my URLs are smaller, hence my need to strip it.

I believe I could do it like this:

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

But this has no practical results on the URLs.

I understand that for it to work I would have to use something like this:

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

but since I'm new to python I'm not quite sure how to do it inside the list I'm appending.

Could you give me a help?



Solution 1:[1]

First you could use print() to see what you get with rstrip()

rstrip('v1/') tries to remove every char v, 1, / at the right end of the string - and they can be in any order.

'something/v1/'.rstrip('v1/') gives 'something'

'something/1111/vvvv/'.rstrip('v1/') gives 'something'

but

'something/v1/other'.rstrip('v1/') gives the same 'something/v1/other'


Maybe you mean split instead strip.

'something/v1/other'.split('v1/') gives list ['something/', 'other']

and you can use [-1] ot get last element and add again 'v1/'

'v1/' + 'something/v1/other'.split('v1/')[-1] gives 'v1/other'

But maybe it could be more useful with / and second argument - how many split to do

url = "https://www.example.com/v1/item/anotheritem/otheritem"

url.split('/')

gives list

['https:', '', 'www.example.com', 'v1', 'item', 'anotheritem', 'otheritem']

but url.split('/', 3) gives

['https:', '', 'www.example.com', 'v1/item/anotheritem/otheritem']

And you can get [-1]

url = "https://www.example.com/v1/item/anotheritem/otheritem"

url.split('/', 3)[-1]

gives

'v1/item/anotheritem/otheritem'

Or maybe you should use urllib.parser.urlsplit for this

url = "https://www.example.com/v1/item/anotheritem/otheritem"

import urllib.parse

urllib.parse.urlsplit(url)

gives

SplitResult(scheme='https', netloc='www.example.com', path='/v1/item/anotheritem/otheritem', query='', fragment='')

and you can get .path

urllib.parse.urlsplit(url).path

gives

'/v1/item/anotheritem/otheritem'

Solution 2:[2]

If all the urls have a path beyond the domain that starts with v1/, one approach would be:

url = 'https://www.example.com/v1/item/anotheritem/otheritem'
part = url[url.index('v1/'):]

Using it in your code:

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"][A["ApiEndpoint"].index('v1/'):] for A in api["ApiEndpoints"]]
            })

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 furas
Solution 2