>>> def chomppy(k):
if k=="": return ""
if k=="\n" or k=="\r\n" or k=="\r": return ""
if len(k)==1: return k #depends on above case being not true
if len(k)==2 and (k[-1]=='\n' or k[-1]=='\r'): return k[0]
#done with weird cases, now deal with average case
lastend=k[-2:] #get last two pieces
if lastend=='\r\n':
outstr=k[:-2]
return outstr
elif (lastend[1]=="\n" or lastend[1]=="\r"):
outstr=k[:-1]
return outstr
return k
>>> chomppy(’cow\n’)
‘cow’
>>> chomppy(”)
”
>>> chomppy(’hat’)
‘hat’
>>> chomppy(’cat\r\n’)
‘cat’
>>> chomppy(’\n’)
”
>>> chomppy(’\r\n’)
”
>>> chomppy(’cat\r’)
‘cat’
>>> chomppy(’\r’)
”
整理 www.blogjava.net/Good-Game