How do convert unicode escape sequences to unicode characters in a python string

Assuming Python sees the name as a normal string, you’ll first have to decode it to unicode: >>> name ‘Christensen Sk\xf6ld’ >>> unicode(name, ‘latin-1′) u’Christensen Sk\xf6ld’ Another way of achieving this: >>> name.decode(‘latin-1′) u’Christensen Sk\xf6ld’ Note the “u” in front of the string, signalling it is uncode. If you print this, the accented letter is … Read more

UnicodeDecodeError: ‘utf8’ codec can’t decode bytes in position 3-6: invalid data

The string you’re trying to parse as a JSON is not encoded in UTF-8. Most likely it is encoded in ISO-8859-1. Try the following: json.loads(unicode(opener.open(…), “ISO-8859-1”)) That will handle any umlauts that might get in the JSON message. You should read Joel Spolsky’s The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode … Read more

What is the difference between isinstance(‘aaa’, basestring) and isinstance(‘aaa’, str)?

In Python versions prior to 3.0 there are two kinds of strings “plain strings” and “unicode strings”. Plain strings (str) cannot represent characters outside of the Latin alphabet (ignoring details of code pages for simplicity). Unicode strings (unicode) can represent characters from any alphabet including some fictional ones like Klingon. So why have two kinds … Read more

Is there a convenient way to apply a lookup table to a large array in numpy?

You can just use image to index into lut if lut is 1D. Here’s a starter on indexing in NumPy: http://www.scipy.org/Tentative_NumPy_Tutorial#head-864862d3f2bb4c32f04260fac61eb4ef34788c4c In [54]: lut = np.arange(10) * 10 In [55]: img = np.random.randint(0,9,size=(3,3)) In [56]: lut Out[56]: array([ 0, 10, 20, 30, 40, 50, 60, 70, 80, 90]) In [57]: img Out[57]: array([[2, 2, 4], … Read more

How do you use subprocess.check_output() in Python?

The right answer (using Python 2.7 and later, since check_output() was introduced then) is: py2output = subprocess.check_output([‘python’,’py2.py’,’-i’, ‘test.txt’]) To demonstrate, here are my two programs: py2.py: import sys print sys.argv py3.py: import subprocess py2output = subprocess.check_output([‘python’, ‘py2.py’, ‘-i’, ‘test.txt’]) print(‘py2 said:’, py2output) Running it: $ python3 py3.py py2 said: b”[‘py2.py’, ‘-i’, ‘test.txt’]\n” Here’s what’s wrong … Read more

deneme bonusu veren sitelerbahis casinomakrobetceltabetpinbahispolobetpolobet girişpinbahis girişmakrobet girişpulibet girişmobilbahis girişkolaybet giriş