Have been searching how to convert a dictionary to a string. But the results found is not the ones i am looking for.
The results i currently find is basically turning the whole dictionary into a string, but i don’t really want {}, , and : in my string.
So i got a dictionary of sorted URLvars i wanted to join together again.
vars = {"id":"2", "test":"3"}
so i want a way to convert this dictionary to:
vars = "id=2&test=3"
UPDATE:
Thanks for the help. Here is also the POC i made for the larger script i am doing! Might be better ways to write this tho ;P
from urlparse import urlparse
import urllib
def main(url, var, params):
urllenght = len(url)
urlfind = url.find("?")
lenght = urlfind - urllenght
params = '&'.join(["%s=%s" % (k, v) for k, v in params.iteritems()])
url = url[:lenght]
print params
print url
print url+"?"+params
if __name__ == '__main__':
url="http://test.com/index.php?id=2&test=2"
url2 = urlparse(url)
params = dict([part.split('=') for part in url2[4].split('&')])
print "Avaible URL params"
for k,v in params.items():
print "Params: %s" % k
while True:
var = raw_input("Select param: ")
if var in params:
main(url, var, params)
break
else:
pass
What you need, is
urllib.urlencode:But keep in mind, that it will also convert special URL symbols to their %-form. So, if you don’t want that, try simply: