Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6080839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:06:26+00:00 2026-05-23T11:06:26+00:00

If I have an object like: d = {‘a’:1, ‘en’: ‘hello’} …then I can

  • 0

If I have an object like:

d = {'a':1, 'en': 'hello'}

…then I can pass it to urllib.urlencode, no problem:

percent_escaped = urlencode(d)
print percent_escaped

But if I try to pass an object with a value of type unicode, game over:

d2 = {'a':1, 'en': 'hello', 'pt': u'olá'}
percent_escaped = urlencode(d2)
print percent_escaped # This fails with a UnicodeEncodingError

So my question is about a reliable way to prepare an object to be passed to urlencode.

I came up with this function where I simply iterate through the object and encode values of type string or unicode:

def encode_object(object):
  for k,v in object.items():
    if type(v) in (str, unicode):
      object[k] = v.encode('utf-8')
  return object

This seems to work:

d2 = {'a':1, 'en': 'hello', 'pt': u'olá'}
percent_escaped = urlencode(encode_object(d2))
print percent_escaped

And that outputs a=1&en=hello&pt=%C3%B3la, ready for passing to a POST call or whatever.

But my encode_object function just looks really shaky to me. For one thing, it doesn’t handle nested objects.

For another, I’m nervous about that if statement. Are there any other types that I should be taking into account?

And is comparing the type() of something to the native object like this good practice?

type(v) in (str, unicode) # not so sure about this...

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T11:06:27+00:00Added an answer on May 23, 2026 at 11:06 am

    You should indeed be nervous. The whole idea that you might have a mixture of bytes and text in some data structure is horrifying. It violates the fundamental principle of working with string data: decode at input time, work exclusively in unicode, encode at output time.

    Update in response to comment:

    You are about to output some sort of HTTP request. This needs to be prepared as a byte string. The fact that urllib.urlencode is not capable of properly preparing that byte string if there are unicode characters with ordinal >= 128 in your dict is indeed unfortunate. If you have a mixture of byte strings and unicode strings in your dict, you need to be careful. Let’s examine just what urlencode() does:

    >>> import urllib
    >>> tests = ['\x80', '\xe2\x82\xac', 1, '1', u'1', u'\x80', u'\u20ac']
    >>> for test in tests:
    ...     print repr(test), repr(urllib.urlencode({'a':test}))
    ...
    '\x80' 'a=%80'
    '\xe2\x82\xac' 'a=%E2%82%AC'
    1 'a=1'
    '1' 'a=1'
    u'1' 'a=1'
    u'\x80'
    Traceback (most recent call last):
      File "<stdin>", line 2, in <module>
      File "C:\python27\lib\urllib.py", line 1282, in urlencode
        v = quote_plus(str(v))
    UnicodeEncodeError: 'ascii' codec can't encode character u'\x80' in position 0: ordinal not in range(128)
    

    The last two tests demonstrate the problem with urlencode(). Now let’s look at the str tests.

    If you insist on having a mixture, then you should at the very least ensure that the str objects are encoded in UTF-8.

    ‘\x80’ is suspicious — it is not the result of any_valid_unicode_string.encode(‘utf8’).
    ‘\xe2\x82\xac’ is OK; it’s the result of u’\u20ac’.encode(‘utf8’).
    ‘1’ is OK — all ASCII characters are OK on input to urlencode(), which will percent-encode such as ‘%’ if necessary.

    Here’s a suggested converter function. It doesn’t mutate the input dict as well as returning it (as yours does); it returns a new dict. It forces an exception if a value is a str object but is not a valid UTF-8 string. By the way, your concern about it not handling nested objects is a little misdirected — your code works only with dicts, and the concept of nested dicts doesn’t really fly.

    def encoded_dict(in_dict):
        out_dict = {}
        for k, v in in_dict.iteritems():
            if isinstance(v, unicode):
                v = v.encode('utf8')
            elif isinstance(v, str):
                # Must be encoded in UTF-8
                v.decode('utf8')
            out_dict[k] = v
        return out_dict
    

    and here’s the output, using the same tests in reverse order (because the nasty one is at the front this time):

    >>> for test in tests[::-1]:
    ...     print repr(test), repr(urllib.urlencode(encoded_dict({'a':test})))
    ...
    u'\u20ac' 'a=%E2%82%AC'
    u'\x80' 'a=%C2%80'
    u'1' 'a=1'
    '1' 'a=1'
    1 'a=1'
    '\xe2\x82\xac' 'a=%E2%82%AC'
    '\x80'
    Traceback (most recent call last):
      File "<stdin>", line 2, in <module>
      File "<stdin>", line 8, in encoded_dict
      File "C:\python27\lib\encodings\utf_8.py", line 16, in decode
        return codecs.utf_8_decode(input, errors, True)
    UnicodeDecodeError: 'utf8' codec can't decode byte 0x80 in position 0: invalid start byte
    >>>
    

    Does that help?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Problem is: Chrome automatically sorts properties of object. If I have an object like:
Does anybody know, if i have an object like $('#input') how can I get
I'm just wondering .. I have and object like this public class Entry{ public
I have an on object like so var obj = {}; function set() {
if I have an XML object like this: <a> <u date=2009-04-10 value=543/> <u date=2009-04-11
I have an Object CaseNote which has numerous child object's like CaseNoteContactType . For
I have a LINQ query that returns some object like this... var query =
I have a script which creates a user-defined object like this: obj = new
I have an object tree that looks something like Ball / \ LegalBall IllegalBall
I have an object for which I'd like to track the number of threads

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.