python rstrip or remove end of line character pattern

I am trying to break the end of the lines in this column. I saw how to redirect a specific character or a given number of characters at the end of a line, but how do you do this based on a pattern?

I would like to remove the entire end of the rows in the column 'team'where we see the lower case, followed by upper case. Then remove the beginning in uppercase. I would like the column 'team':

   team                              pts/g
St. Louis RamsSt. Louis             32.875
Washington RedskinsWashington       27.6875
Minnesota VikingsMinnesota          24.9375
Indianapolis ColtsIndianapolis      26.4375
Oakland RaidersOakland              24.375
Carolina PanthersCarolina           26.3125
Jacksonville JaguarsJacksonville    24.75
Chicago BearsChicago                17.0
Green Bay PackersGreen Bay          22.3125
San Francisco 49ersSan Francisco    18.4375
Buffalo BillsBuffalo                20.0

look like this:

   team                              pts/g
St. Louis Rams                      32.875
Washington Redskins                 27.6875
Minnesota Vikings                   24.9375
Indianapolis Colts                  26.4375
Oakland Raiders                     24.375
Carolina Panthers                   26.3125
Jacksonville Jaguars                24.75
Chicago Bears                       17.0
Green Bay Packers                   22.3125
San Francisco 49ers                 18.4375
Buffalo Bills                       20.0
+6
source share
2 answers

You can use re.sub(pattern, repl, string)for this.

Let me use this regex to match:

([a-z])[A-Z].*?(  )

([a-z]), [A-Z] .*?, ( ). , \1 \2 re.sub:

new_text = re.sub(r"([a-z])[A-Z].*?(  )", r"\1\2", text)

:

   team                              pts/g
St. Louis Rams             32.875
Washington Redskins       27.6875
Minnesota Vikings          24.9375
Indianapolis Colts      26.4375
Oakland Raiders              24.375
Carolina Panthers           26.3125
Jacksonville Jaguars    24.75
Chicago Bears                17.0
Green Bay Packers          22.3125
San Francisco 49ers    18.4375
Buffalo Bills                20.0

. , , , re.sub, Match a str:

def replace_with_spaces(match):
    return match.group(1) + " "*len(match.group(2)) + match.group(3)

( , , , ):

new_text re.sub(r"([a-z])([A-Z].*?)(  )", replace_with_spaces, text)

:

   team                              pts/g
St. Louis Rams                      32.875
Washington Redskins                 27.687
Minnesota Vikings                   24.937
Indianapolis Colts                  26.437
Oakland Raiders                     24.375
Carolina Panthers                   26.312
Jacksonville Jaguars                24.75
Chicago Bears                       17.0
Green Bay Packers                   22.312
San Francisco 49ers                 18.437
Buffalo Bills                       20.0
+5

, : - , 2 , . , . ... . :

def smart_rstrip ( s ):
    for i in xrange( 1,len( s )):
        if s.endswith( s[:i] ):
            return s[:-i]
    return s


s = ['St. Louis RamsSt. Louis', 'Washington RedskinsWashingt...]
print '\n'.join( s )
print '\n'.join( map( smart_rstrip,s ))

, , , ...

0

All Articles