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)

(Diff.) ← Nächstältere Version | Aktuelle Version (Diff.) | Nächstjüngere Version → (Diff.)
Wechseln zu: Navigation, Suche

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/.

  1. str is for bytes, NOT strings
  2. unicode is for strings
  3.  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
  4. 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 or decode() on Unicode objects.
Quelle: Offene Naturführer, Das Wiki zu Bestimmungsfragen: Benutzer:Andreas Plank/Python (Zuletzt geändert:
Dieses Attribut ist ein Spezialattribut in diesem Wiki.
2 Juni 2020 10:07:37). Abgerufen am 23. Dezember 2024, 07:42 von https://offene-naturfuehrer.de/web/Benutzer:Andreas_Plank/Python