JFerro
JFerro

Reputation: 3433

Create and urlparse an URL that has to include a variable being a list of strings

I am using the following code to parse an URL:

url3 = "https://flyingcar.org/dfr/?d=34u&f=56&dossier=ZT18174&document=US1234567&docs='US1','US2','US3'#{'para1':'UK','para2':'Ireland'}"

parsed = urlparse(url3)
print('url:\n',url3)

captured_values = parse_qs(parsed.query)
print(f'parse_qs of querys: {captured_values} type: {type(captured_values)}')
print(f'here the parameters: {parsed.fragment} type: {type(parsed.fragment)}')

It all work kind of fine. What I don't know is how to build the URL in order to pass a list of variables, i.e. in the above example the results are:

url:
 https://flyingcar.org/dfr/?d=34u&f=56&dossier=ZT18174&document=US1234567&docs='US1','US2','US3'#{'para1':'UK','para2':'Ireland'}
parsed.params=''
parsed.query="d=34u&f=56&dossier=ZT18174&document=US1234567&docs='US1','US2','US3'"
parsed.fragment="{'para1':'UK','para2':'Ireland'}"
parse_qs of querys: {'d': ['34u'], 'f': ['56'], 'dossier': ['ZT18174'], 'document': ['US1234567'], 'docs': ["'US1','US2','US3'"]} type: <class 'dict'>
here the parameters: {'para1':'UK','para2':'Ireland'} type: <class 'str'>

I would like to that in the query the variable docs contains a list and not a string, like:

'docs': ['US1','US2','US3']
#instead of:
'docs': ["'US1','US2','US3'"]

How should I build the URL?

Upvotes: 1

Views: 141

Answers (1)

user9613901
user9613901

Reputation:

You can use this snippet after parsing URL:

for i in captured_values:
    captured_values[i]=captured_values[i][0].replace("'","").split(",")

print(captured_values)

output:

{'d': ['34u'], 'f': ['56'], 'dossier': ['ZT18174'], 'document': ['US1234567'], 'docs': ['US1', 'US2', 'US3']}

Although I do not feel good about replacing it, but you can use the solution

Upvotes: 1

Related Questions