build php-style URLs with arrays - with Python -
an array encoded in url php uses [] notation, e.g.
$f = array( 'a', 'b', 'c'); http_build_query( array('f'=> $f) ); http://server/q.php?f[]=aaaa&f[]=bbb&f[]=ccc
in python haven't seen such notation. instead, saw:
f = [ 'a', 'b', 'c'] http://server/q.py?f=aaaa&f=bbb&f=ccc
is there ready-made method in python encode arrays php chew on ?
edit
addressing specific comments i've seen: following lines not interpreted same way in php. run phpinfo() if don't believe me, @daniel:
test.php?f=1&f=zzzzz&f=b test.php?f[]=1&f[]=zzzzz&f[]=b
and guy renamed "f" "f[]" through loop: thank you, asking built-in solution or module.
it important me not reinvent wheels, cases query complicated, mixed data types, e.g. { 'str': 'asd123#$', 'arr': [ 'val%', ')(*^&$@$', 'val3', 123.21], 'd': { 'k1': 'v1', 'k2': 'v2'}. happen use larger , more complicated cases api requires them urls.
edit 2
i wonder why question down-voted:
this legitimate question bridging python code , php website through url.
in case: hasoffers.com has api accepts requests in php fashion.
also, mentioned before, url encoding tend complicated.
you can try following:
>>> = 'a b c'.split() >>> ['a', 'b', 'c'] >>> def help_build_query(l): ... return "http://server/q.py?{}=aaaa&{}=bbb&{}=ccc".format(*l) ... >>> help_build_query(a) 'http://server/q.py?a=aaaa&b=bbb&c=ccc' >>>
a
list has been split. {}
used place holders. *
called splat operator, , makes each of variables in list, argument function.
Comments
Post a Comment