Review of some string/list/file functions/methods:
.split():
>>> s='Hi<bleep>there<bleep>Fred'
>>> things=s.split('<bleep>')
>>> print things
['Hi','there','Fred']
>>> another=s.split('<')
>>> print another
['Hi','bleep>there','bleep>Fred']
>>> t='Hi;there;Fred;'
>>> print t.split(';')
['Hi','there','Fred',''] # note the extra blank string at the end of the list
>>> v=' Hi there Fred '
>>> v.split()
['Hi','there','Fred']
.join():
>>> lst=['Hi','there','Fred']
>>> p=','.join(lst)
>>> print p
Hi,there,Fred
>>> print '\n'.join(lst) # notice how print displays strings
Hi
there
Fred
>>> '\n'.join(lst) # notice the difference between using print to display a string, and just asking the shell to display it (as it's doing here)
'Hi\nthere\nFred'
File reading/writing:
def FileReading(filename):
# read a file and return a list of lines in the file
try:
f=open(filename,'r')
s=f.read()
f.close()
except:
s=''
lines=s.split('\n')
# remove any trailing blank lines
while len(lines)>0 and lines[-1]=='':
lines=lines[:-1]
return lines
def FileWriting(filename,stuff):
# write stuff (a string) to a file
try:
f=open(filename,'w')
f.write(stuff)
f.close()
except:
return False
return True