Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pass in list variable in url string witout "[ ] '" in python

I need to format a url string for using with urllib: the url string I want to get are as:

http://localhost:8086/service/records/names?name=A,B,C,D,E,F,G

if I use

namelist = ['A','B','C','D','E','F','G']
url = 'http://localhost:8086/service/records/names?name={namelist}'.format(namelist=namelist}

then I get:

http://localhost:8086/service/records/names?name=['A','B','C','D','E','F','G']

so how should I format an url string by passing in a string list wihout "[]'"?

like image 769
user9571515 Avatar asked May 19 '26 06:05

user9571515


1 Answers

Your first option is what the other answers suggest: to create your comma-separated list yourself, like so:

import urllib.parse
query_string = 'name=' + ','.join(namelist)
url = 'http://localhost:8086/service/records/names?{query_string}'.format(query_string=query_string)
# url == 'http://localhost:8086/service/records/names?name=A,B,C,D,E,F,G'

This fits what you asked for, however it has some limitations: first, if one of your names has a comma, it will not be correctly escaped. Second, the commas in your list, and other characters in namelist won't be properly encoded for the URL.

Your second option, a more robust version of the previous one, is to encode your list, like so:

import urllib.parse
query_params = {'name': ','.join(namelist)}
query_string = urllib.parse.urlencode(query_params)
url = 'http://localhost:8086/service/records/names?{query_string}'.format(query_string=query_string)
# url == 'http://localhost:8086/service/records/names?name=A%2CB%2CC%2CD%2CE%2CF%2CG'

This will properly escape the characters for URL usage, but you are still left with the manual assembling and parsing of the query string.

There is a third option, which I would suggest: use the standard way of passing a list in the query string, which is to repeat the key.

import urllib.parse
query_params = {'name': namelist}
query_string = urllib.parse.urlencode(query_params, doseq=True)
url = 'http://localhost:8086/service/records/names?{query_string}'.format(query_string=query_string)
# url == 'http://localhost:8086/service/records/names?name=A&name=B&name=C&name=D&name=E&name=F&name=G'

This last option, a bit more verbose, is more robust though, as the URL parser will return a list you don't need to parse. Additionally, if there is a comma in one of your names, it will be automatically escaped.

Check out the difference between the three options:

>>> urllib.parse.parse_qs('name=A,B,C,D,E,F,G')
{'name': ['A,B,C,D,E,F,G']}
>>> urllib.parse.parse_qs('name=A%2CB%2CC%2CD%2CE%2CF%2CG')
{'name': ['A,B,C,D,E,F,G']}
>>> urllib.parse.parse_qs('name=A&name=B&name=C&name=D&name=E&name=F&name=G')
{'name': ['A', 'B', 'C', 'D', 'E', 'F', 'G']}

Last one will be easier to work with!

like image 104
Samuel Dion-Girardeau Avatar answered May 22 '26 03:05

Samuel Dion-Girardeau



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!