python rstrip或通过字符模式删除字符串的结尾

chi*_*n88 7 python strip

我试图在此列中删除字符串的结尾.我已经看过如何在一个字符串的末尾rstrip一个特定的字符或一定数量的字符,但是你如何根据一个模式来做呢?

我想删除'team'列中的字符串的整个末尾,我们看到小写字母后面跟着大写字母.然后从大写开始删除.我想要以下'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
Run Code Online (Sandbox Code Playgroud)

看起来像这样:

   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
Run Code Online (Sandbox Code Playgroud)

Fel*_*elk 7

你可以用re.sub(pattern, repl, string)它.

让我们使用这个正则表达式进行匹配:

([a-z])[A-Z].*?(  )
Run Code Online (Sandbox Code Playgroud)

它匹配一个小写字符([a-z]),后跟一个大写字符[A-Z]和任何字符,.*?直到它碰到两个空格( ).小写字符和两个空格在一个组中,因此在使用时可以使用第一组\1\2第二组重新插入它们re.sub:

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

输出示例:

   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
Run Code Online (Sandbox Code Playgroud)

这使得空间调整变得混乱.可能与您无关,但如果您想用空格替换擦除的字符,您可以传递一个函数而不是替换字符串re.sub,它接受一个Match对象并返回一个str:

def replace_with_spaces(match):
    return match.group(1) + " "*len(match.group(2)) + match.group(3)
Run Code Online (Sandbox Code Playgroud)

然后像这样使用它(请注意我如何将待替换的部分放入正则表达式组中):

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

这会产生:

   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
Run Code Online (Sandbox Code Playgroud)