How to get string objects instead of Unicode from JSON?

I’m using Python 2 to parse JSON from ASCII encoded text files. When loading these files with either json or simplejson, all my string values are cast to Unicode objects instead of string objects. The problem is, I have to use the data with some libraries that only accept string objects. I can’t change the … Read more

How to send POST request?

I found this script online: import httplib, urllib params = urllib.urlencode({‘number’: 12524, ‘type’: ‘issue’, ‘action’: ‘show’}) headers = {“Content-type”: “application/x-www-form-urlencoded”, “Accept”: “text/plain”} conn = httplib.HTTPConnection(“bugs.python.org”) conn.request(“POST”, “”, params, headers) response = conn.getresponse() print response.status, response.reason 302 Found data = response.read() data ‘Redirecting to <a href=”http://bugs.python.org/issue12524″>http://bugs.python.org/issue12524</a>’ conn.close() But I don’t understand how to use it with … Read more

Setting the correct encoding when piping stdout in Python

When piping the output of a Python program, the Python interpreter gets confused about encoding and sets it to None. This means a program like this: # -*- coding: utf-8 -*- print u”åäö” will work fine when run normally, but fail with: UnicodeEncodeError: ‘ascii’ codec can’t encode character u’\xa0′ in position 0: ordinal not in … Read more

How to pretty-print a numpy.array without scientific notation and with given precision?

I’m curious, whether there is any way to print formatted numpy.arrays, e.g., in a way similar to this: x = 1.23456 print ‘%.3f’ % x If I want to print the numpy.array of floats, it prints several decimals, often in ‘scientific’ format, which is rather hard to read even for low-dimensional arrays. However, numpy.array apparently … Read more

What is the best way to remove accents (normalize) in a Python unicode string?

I have a Unicode string in Python, and I would like to remove all the accents (diacritics). I found on the web an elegant way to do this (in Java): convert the Unicode string to its long normalized form (with a separate character for letters and diacritics) remove all the characters whose Unicode type is … Read more

What is the difference between dict.items() and dict.iteritems() in Python2?

Are there any applicable differences between dict.items() and dict.iteritems()? From the Python docs: dict.items(): Return a copy of the dictionary’s list of (key, value) pairs. dict.iteritems(): Return an iterator over the dictionary’s (key, value) pairs. If I run the code below, each seems to return a reference to the same object. Are there any subtle … Read more

What exactly do “u” and “r” string flags do, and what are raw string literals?

While asking this question, I realized I didn’t know much about raw strings. For somebody claiming to be a Django trainer, this sucks. I know what an encoding is, and I know what u” alone does since I get what is Unicode. But what does r” do exactly? What kind of string does it result … Read more