Benutzer:Andreas Plank/Python
Aus Offene Naturführer
< Benutzer:Andreas Plank
Version vom 2. Juni 2020, 11:07 Uhr von Andreas Plank (Diskussion | Beiträge)
Dictionaries
mydict = {'one': 0, 'two': 23}
for k, v in mydict.iteritems():
# also mydict.iterkeys() mydict.itervalues()
print "key: %5s; value: %5s" % (k, v)
mydict.keys()
# ['two', 'one']
mydict.values()
# [23, 0]
Install/Setup
# install of a package manually downloaded
sudo python setup.py install --record files_installed.txt
# unistall files (only manually)
sudo rm $(cat files_installed.txt)
# make sure the package directory is removed as well, e.g. /usr/local/lib/python2.6/site-packages/xlrd/
sudo easy_install --record files_installed.txt xlrd
Unicode
Dohler, Derek. 2014. ‘Solving Unicode Problems in Python 2.7’. Azavea (blog). 24 March 2014. http://www.azavea.com/blog/2014/03/24/solving-unicode-problems-in-python-2-7/.
- str is for bytes, NOT strings
- unicode is for strings
- Solution: The Unicode ‘airlock’
- The best way to attack the problem, as with many things in Python, is to be explicit. That means that every string that your code handles needs to be clearly treated as either Unicode or a byte sequence.
- The most systematic way to accomplish this is to make your code into a Unicode-only clean room. That is, your code should only use Unicode objects internally; you may even want to put checks for type< ‘unicode’> in key places to keep yourself honest.
- Then, put ‘airlocks’ at the entry points to your code which will ensure that any byte sequence attempting to enter your code is properly clothed in a protective Unicode bunny suit before being allowed inside. For example:
with f = open('file.txt'): # BAD--gives you bytes
better use:with f = codecs.open('file.txt', encoding='utf-8'): # GOOD--gives you Unicode
- Airlock Construction Kit (Useful Unicode tools)
- Nearly every Unicode problem can be solved by the proper application of these tools; they will help you build an airlock to keep the inside of your code nice and clean:
-
encode()
: Gets you from Unicode -> bytes -
decode()
: Gets you from bytes -> Unicode -
codecs.open(..., encoding="utf-8")
: Read and write files directly to/from Unicode (you can use any encoding, not just utf-8, but utf-8 is most common). -
u"..."
: Makes your string literals into Unicode objects rather than byte sequences.
-
- Warning: Don’t use
encode()
on bytes ordecode()
on Unicode objects.
- Nearly every Unicode problem can be solved by the proper application of these tools; they will help you build an airlock to keep the inside of your code nice and clean: