title: Python urlparse.parse_qs unicode url
body: <p><code>urlparse.parse_qs</code> is usefull for parsing url parameters, and it works fine with simple ASCII url, represented by <code>str</code>. So i can parse a query and then construct the same path using <code>urllib.urlencode</code> from parsed data:</p>
  
  <pre><code>&gt;&gt;&gt; import urlparse
  &gt;&gt;&gt; import urllib
  &gt;&gt;&gt;
  &gt;&gt;&gt; path = '/?key=value' #path is str
  &gt;&gt;&gt; query = urlparse.urlparse(path).query
  &gt;&gt;&gt; query
  'key=value'
  &gt;&gt;&gt; query_dict = urlparse.parse_qs(query)
  &gt;&gt;&gt; query_dict
  {'key': ['value']}
  &gt;&gt;&gt; '/?' + urllib.urlencode(query_dict, doseq=True)
  '/?key=value' # &lt;-- path is the same here
  </code></pre>
  
  <p>It also works fine, when url contains percent encoded non-ASCII param:</p>
  
  <pre><code>&gt;&gt;&gt; value = urllib.quote(u'значение'.encode('utf8'))
  &gt;&gt;&gt; value
  '%D0%B7%D0%BD%D0%B0%D1%87%D0%B5%D0%BD%D0%B8%D0%B5'
  &gt;&gt;&gt; path = '/?key=%s' % value
  &gt;&gt;&gt; path
  '/?key=%D0%B7%D0%BD%D0%B0%D1%87%D0%B5%D0%BD%D0%B8%D0%B5'
  &gt;&gt;&gt; query = urlparse.urlparse(path).query
  &gt;&gt;&gt; query
  'key=%D0%B7%D0%BD%D0%B0%D1%87%D0%B5%D0%BD%D0%B8%D0%B5'
  &gt;&gt;&gt; query_dict = urlparse.parse_qs(query)
  &gt;&gt;&gt; query_dict
  {'key': ['\xd0\xb7\xd0\xbd\xd0\xb0\xd1\x87\xd0\xb5\xd0\xbd\xd0\xb8\xd0\xb5']}
  
  &gt;&gt;&gt; '/?' + urllib.urlencode(query_dict, doseq=True)
  '/?key=%D0%B7%D0%BD%D0%B0%D1%87%D0%B5%D0%BD%D0%B8%D0%B5'  # &lt;-- path is the same here
  </code></pre>
  
  <p>But, when using django, i get the url using <code>request.get_full_path()</code>, and it returns path as <code>unicode</code> string:</p>
  
  <pre><code>&gt;&gt;&gt; path = request.get_full_path()
  &gt;&gt;&gt; path
  u'/?key=%D0%B7%D0%BD%D0%B0%D1%87%D0%B5%D0%BD%D0%B8%D0%B5' # path is unicode
  </code></pre>
  
  <p>Look what will happen now:</p>
  
  <pre><code>&gt;&gt;&gt; query = urlparse.urlparse(path).query
  &gt;&gt;&gt; query
  u'key=%D0%B7%D0%BD%D0%B0%D1%87%D0%B5%D0%BD%D0%B8%D0%B5'
  &gt;&gt;&gt; query_dict = urlparse.parse_qs(query)
  &gt;&gt;&gt; query_dict
  {u'key': [u'\xd0\xb7\xd0\xbd\xd0\xb0\xd1\x87\xd0\xb5\xd0\xbd\xd0\xb8\xd0\xb5']}
  &gt;&gt;&gt;
  </code></pre>
  
  <p><code>query_dict</code> contains unicode string, that contains bytes! Not unicode points!
  And of course i've got a UnicodeEncodeError, when trying to urlencode that string:</p>
  
  <pre><code>&gt;&gt;&gt; urllib.urlencode(query_dict, doseq=True)
  Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
    File "C:\Python27\Lib\urllib.py", line 1337, in urlencode
      l.append(k + '=' + quote_plus(str(elt)))
  UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-15: ordinal not in range(128)
  </code></pre>
  
  <p>Currently i have a solution:</p>
  
  <pre><code># just convert path, returned by request.get_full_path(), to `str` explicitly:
  path = str(request.get_full_path())
  </code></pre>
  
  <p>So the questions are:</p>
  
  <ul>
  <li>why parse_qs return so strange string (unicode, that contains bytes)?</li>
  <li>is it safe to convert url to str?</li>
  </ul>