user9571515
user9571515

Reputation: 349

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 "[]'"?

Upvotes: 3

Views: 4689

Answers (4)

Samuel Dion-Girardeau
Samuel Dion-Girardeau

Reputation: 3170

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!

Upvotes: 4

Bram Vanroy
Bram Vanroy

Reputation: 28473

In addition to the other answers: you can also use the nice F-string feature of Python 3. It's a lot prettier than .format() imo, and reminds you of other programming languages that allow variable interpolation.

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

print(url)
# http://localhost:8086/service/records/names?name=A,B,C,D,E,F,G

Upvotes: 1

James Lingham
James Lingham

Reputation: 439

.format(namelist=",".join(namelist))

will work, using "," between list entries

Upvotes: 1

Max
Max

Reputation: 739

Join the list into a string with...

'[insert seperator here]'.join(namelist)

so in your case

','.join(namelist) this produces 'A,B,C,D,E,F'...

Then you can use your initial method with the .format()

Upvotes: 2

Related Questions