阿波罗的战车
这有帮助吗?import rebefore = "2369 EMILIA MEL [5]Res."after = re.sub(r"\[.*", "", before).strip()print(before)print(after)哪个打印:2369 EMILIA MEL [5]Res.2369 EMILIA MEL更新根据您的评论,您希望删除[或(删除所有内容:import rebefore = [ "2369 EMILIA MEL [5]Res.", "2360 DYNA REAVER (NSW)[5]"]after = [re.sub(r"(\(|\[).*", "", b).strip() for b in before]print(before)print(after)哪个打印:['2369 EMILIA MEL [5]Res.', '2360 DYNA REAVER (NSW)[5]']['2369 EMILIA MEL', '2360 DYNA REAVER']更新 2这是一个如何在 a 上使用它的示例pandas.DataFrame:import pandas as pdimport redf = pd.DataFrame({"before": [ "CROSS THREADED", "ALL ABOUT ARBY [5]Res.", "EMILIA MEL [5]Res.", "DYNA REAVER (NSW)[5]", "PARTY MIX [5]",]})df["after"] = df["before"].apply( lambda s: re.sub(r"(\(|\[).*", "", s).strip())print(df)哪个打印: before after0 CROSS THREADED CROSS THREADED1 ALL ABOUT ARBY [5]Res. ALL ABOUT ARBY2 EMILIA MEL [5]Res. EMILIA MEL3 DYNA REAVER (NSW)[5] DYNA REAVER4 PARTY MIX [5] PARTY MIX更新 3正如评论中指出的那样,您也可以pandas.Series.str.replace直接使用:df["after"] = df["before"].str.replace(r"( )*(\(|\[).*", "")