python - chomp

python - chomp
转自: http://markbieda.wordpress.com/2008/08/13/python-and-bioinformatics-and-perl-chomp-in-python/
>>>   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

你可能感兴趣的:(python - chomp)