CodeTalk
CodeTalk

Reputation: 3667

Replace resulting values with python split method

What I'm trying to do with a list of path's looking like:

['path/to/somewhere?id=6&name=Test', 'path/to/list?date=05222014&type=cal']

my puesdocode:

for each path with parameters:
    split path on character ? and &
    replace value after "=" with a value from self.fuzz_vectors

My current code:

def parse_paths(self):
    dictpath = []
    if self.path_object is not None:
        for path in self.path_object:
            self.params = path.split("?")[1].split("&") #split value after ? and after &
            #replace splitted string after '=' sign with self.value_path
        dictpath.append(self.params)

My code is somewhat there, but I'm a bit confused how to split the string after the equals sign as commented.

Any help is greatly appreciated.

Thank you.

Upvotes: 0

Views: 64

Answers (2)

Marcin
Marcin

Reputation: 238957

Not sure if you it must involve only split method and it cant involve any other string replacement methods or regular expressions. Anyway, this is one way of doing it without using any regular expressions or string replace method:

st = ['path/to/somewhere?id=6&name=Test', 'path/to/list?date=05222014&type=cal']

dictpath = {}

for path in st:
    params = path.split("?")[1].split("&") 
    out = list(map(lambda v: v.split("=")[0] +"=" + fuzz_vectors, params))
    dictpath[path] = out

print(dictpath)

gives:

{'path/to/list?date=05222014&type=cal': ['date=some_string', 'type=some_string'],    'path/to/somewhere?id=6&name=Test': ['id=some_string', 'name=some_string']}

Upvotes: 1

Pavel
Pavel

Reputation: 7562

you might want to look at urlparse module:

>>> import urlparse
>>> print urlparse.parse_qs('path/to/somewhere?id=6&name=Test'.split('?')[1])
{'id': ['6'], 'name': ['Test']}
>>> print urlparse.parse_qs('path/to/list?date=05222014&type=cal'.split('?')[1])
{'date': ['05222014'], 'type': ['cal']}

then you can replace the values just by addressing the dict keys

Upvotes: 0

Related Questions