4 回答
TA贡献1862条经验 获得超6个赞
这有帮助吗?
import re
before = "2369 EMILIA MEL [5]Res."
after = re.sub(r"\[.*", "", before).strip()
print(before)
print(after)
哪个打印:
2369 EMILIA MEL [5]Res.
2369 EMILIA MEL
更新
根据您的评论,您希望删除[或(删除所有内容:
import re
before = [
"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 pd
import re
df = 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 after
0 CROSS THREADED CROSS THREADED
1 ALL ABOUT ARBY [5]Res. ALL ABOUT ARBY
2 EMILIA MEL [5]Res. EMILIA MEL
3 DYNA REAVER (NSW)[5] DYNA REAVER
4 PARTY MIX [5] PARTY MIX
更新 3
正如评论中指出的那样,您也可以pandas.Series.str.replace直接使用:
df["after"] = df["before"].str.replace(r"( )*(\(|\[).*", "")
TA贡献1865条经验 获得超7个赞
这可能对你有帮助。
def trim(string):
string = string.split('(')[0]
string = string.split('[')[0]
return string.strip()
df['CROSS THREADED'] = df['CROSS THREADED'].apply(lambda row: trim(string=row))
添加回答
举报