Hello! This is the official page of Wayne Sebastian Pereanu.

Software > Cheat Sheets > Python


Opening a file
f=open("test.txt", "r")
for line in f:
	thisline = line.replace("\n","")
f.close()

>Writing to a file
f=open("test.txt", "w")
f.write("test text")
f.close()

Getting string position of regex matches within a string
thisline = 'a:2:{s:4:"foo";s:3:"bar";s:5:"label";s:10:"Choice "C"";}';
pattern = re.compile( r's:\b\d+\b:"' )
for str_pos in pattern.finditer(thisline):
	print str_pos.start(), str_pos.group()

Split string into array (explode)
your_string.split(your_separator)

Case-insensitive "in" Case-sensitive
if this_entry not in main_table:
Case-insensitive
if this_entry.lower() not in (each_main_entry.lower() for each_main_entry in main_table):