Articles Tagged unicode

Unicode string formatting

Did you know, if one of values in string formatting expression with % operator is unicode, then result string will also be unicode?

>>> "Hello, %s" % u"Alex"
u'Hello, Alex'
>>> "Hello, %s" % u"Алексей"
u'Hello, \u0410\u043b\u0435\u043a\u0441\u0435\u0439'

I used to work with .format string method and its behavior is more attractive to me: type of source string is saved and if some parameter contains non-ascii symbols, UnicodeEncodeError exception is raised ...

Posted on 2013-06-20

Parse url which contains unicode query, using urlparse.parse_qs

Task: get dictionary of URL GET query. For example, we have following url:

it is needed to get a dict:

{'key': ['value'], 'a': ['b']}

Values are lists, because one key may have several values:

Out: {'key': ['value'], 'a': ['b', 'c']}

In python there is a function urlparse.parse_qs for that purpose:

>>> import urlparse
>>> query = "key=value&a=b ...

Posted on 2013-05-22