title: Parsing query parameters in Python
body: <p>The problem lies somewhere in how I'm parsing and or reassembling urls.  I'm losing the <code>?id=1</code> and <code>getting ?d=1</code>.</p>
  
  <p>What I am trying to do is have the ability to manipulate and query parameter and reassemble it before sending back out modified. Meaning the dictionaries would be modified than using <code>urlencode(modified_dict)</code> I would reassemble url + query.</p>
  
  <p>Can someone give me a pointer on what I'm doing wrong here. </p>
  
  <pre><code>from urlparse import parse_qs, urlparse , urlsplit
  from urllib import urlencode
  import os
  import sys
  import mechanize
  from collections import OrderedDict
  import urllib2
  scrape_post_urls = []
  get_inj_tests = []
  
  #check multiple values to  strip out duplicate and useless checks
  def parse_url(url):
      parsed = urlparse(url,allow_fragments=False)
  
      if parsed.query:
  
  
          if url not in get_inj_tests:
             get_inj_tests.append(url)
             #print url
             '''get_inj_tests.append(url)
             print url
             #print 'scheme  :', parsed.scheme
             #print 'netloc  :', parsed.netloc
             print 'path    :', parsed.path
             print 'params  :', parsed.params
             print 'query   :', parsed.query
             print 'fragment:', parsed.fragment
             #print 'hostname:', parsed.hostname, '(netloc in lower case)'
             #print 'port    :', parsed.port
             '''
  else:
      if url not in scrape_post_urls:
         scrape_post_urls.append(url)
         #print url
  
  
  
  
  def main():
      unparsed_urls = open('in.txt','r')
      for urls in unparsed_urls:
          try:
             parse_url(urls)
          except:
              pass
  
      print(len(scrape_post_urls))
      print(len(get_inj_tests))
      clean_list = list(OrderedDict.fromkeys(get_inj_tests))
      reaasembled_url = ""
      #print clean_list
      for query_test in clean_list:
          url_object = urlparse(query_test,allow_fragments=False)
          #parse query paramaters
          url = query_test.split("?")[1]
          dicty = {x[0] : x[1] for x in [x.split("=") for x in url[1:].split("&amp;") ]}
          query_pairs = [(k,v) for k,vlist in dicty.iteritems() for v in vlist]
          reaasembled_url = "http://" + str(url_object.netloc) + str(url_object.path) +  '?'
          reaasembled_query = urlencode(query_pairs)
          full_url = reaasembled_url + reaasembled_query
          print dicty
  
  
  
  
  main()
  </code></pre>