You may optionally enter a comparison operator (<, <=, >, >=, <> or =) at the beginning of each of your search values to specify how the comparison should be done.
try:
print 1/0
except Exception as e:
print traceback.extract_stack()
else:
print "this is the else block"
finally:
print "finally clean up or something"
161
Python
Strings
String nach einem Teilstring durchsuchen
# http://www.tutorialspoint.com/python/string_find.htm
# this function is case sensitive
fullstring = "This is my string"
searchFor = "my"
startPosition = fullstring.find(searchFor, 0, len(fullstring))
# startPosition will have 8
if startPosition>=0:
print "I found it!"
startPosition = fullstring.upper().find(searchFor.upper()) # case insensitive
import pprint
a="This is a test"
b=1
c=True
pprint.pprint(locals())
#{'__builtins__': <module '__builtin__' (built-in)>,
# '__doc__': None,
# '__name__': '__main__',
# '__package__': None,
# 'a': 'This is a test',
# 'b': 1,
# 'c': True,
# 'pprint': <module 'pprint' from '/usr/lib/python2.7/pprint.pyc'>}
pprint.pprint(globals())
166
Python
Filehandling
Datei auf dem Betriebssystem ausführen
import commands
output=commands.getoutput("dir")
print output
# home tmp myFile.sh
170
Python
Datum, Zeit
Gestriges Datum
from datetime import date, timedelta
d=date.today()-timedelta(days=1)
output=d.isoformat()
# 2014-02-27
186
Python
Strings
String nach Delimenter zerlegen
myString = "A;B;C;D;E"
myPieces = myString.split(";") # erzeugt ein Arrey mit den einzelnen Zeichen
print myPieces[2] # gibt C aus
188
Python
Prozeduren, Funktionen, Methoden
Deklaration einer Funktion/Prozedur
def this_is_my_function_name(param1):
print "do something inside the function " + param1
print "do something outside the function"
# call the function
this_is_my_function_name("hello world")
189
Python
Schleifen
For-Schleife
for i in range(0, 10):
i=1
time.sleep(1)
print i
items = ['computer', 'fruits', 'fraggels']
# without indexes
for item in items:
print item
# with indexes
for (i, item) in enumerate(items):
print i, item