![](/img/trans.png)
[英]Python, extracting a string between two specific characters for all rows in a dataframe
[英]Python - Extracting text in a string between two other specific characters?
我有各種各樣的文本字符串,其中包含用戶名,公司名稱和電話號碼,它們都類似於以下內容:
FirstName LastName (Some Business Name / phoneNumber)
FirstName LastName (Business Name / phoneNumber)
FirstName LastName (BusinessName / differentphoneNumber)
FirstName LastName (Short Name / somephoneNumber)
FirstName LastName (Very Long Business Name / otherphoneNumber)
現實世界中的示例可能如下所示:
David Smith (Best Pool and Spa Supplies / 07438473784)
Bessy McCarthur Jone (Dog Supplies / 0438-343522)
我已經使用此代碼提取了名字(如我之前所需要的),並且效果很好:
import re
details = re.findall(r'^[\w+]+', input_data['stripeDescription'])
return {
'firstName': details[0] if details else None\``
}
我該如何找到左括號“(”和正斜杠“ /”之間的文本,然后檢索公司名稱?
這可能不是一個完美的解決方案,但效果很好:)
s1='David Smith (Best Pool and Spa Supplies / 07438473784)'
sp1=s1.split('(')
sp2=sp1[1].split('/')
print(sp2)
輸出:['最佳泳池和水療用品','07438473784)']
使用括號將要匹配的模式分組到用於re.findall
的正則表達式中:
s = '''David Smith (Best Pool and Spa Supplies / 07438473784)
Bessy McCarthur Jone (Dog Supplies / 0438-343522)'''
import re
print(re.findall(r'\(([^/]+?) */', s))
輸出:
['Best Pool and Spa Supplies', 'Dog Supplies']
這是相當健壯的,但是不會處理帶有括號的名稱。 即,它期望第一個(
分隔名稱)。但是,通過注意該企業中有\\).*\\(
,您可能就能知道出了點問題\\).*\\(
。
data = """
David Smith (Best Pool and Spa Supplies / 07438473784)
David Smith2 (Best Pool/Spa Supplies / 07438473784)
Bessy McCarthur Jone (Dog Supplies / 0438-343522)
Bessy McCarthur Jone2 (Dog (and cat) Supplies / 0438-343522)
Bessy (Bess, fails) McCarthur Jone3 (Dog Supplies / 0438-343522)
"""
lines = [line.strip() for line in data.splitlines() if line.strip()]
for line in lines:
name,rest = line.split("(",1)
name = name.strip()
phone = rest.rsplit("/")[1].replace(")","").strip()
biz = rest.rsplit("/",1)[0].strip()
print("\n "+line)
print(" =>name:%s: phone:%s:biz:%s:" % (name, phone,biz))
輸出:
David Smith (Best Pool and Spa Supplies / 07438473784)
=>name:David Smith: phone:07438473784:biz:Best Pool and Spa Supplies:
David Smith2 (Best Pool/Spa Supplies / 07438473784)
=>name:David Smith2: phone:Spa Supplies:biz:Best Pool/Spa Supplies:
Bessy McCarthur Jone (Dog Supplies / 0438-343522)
=>name:Bessy McCarthur Jone: phone:0438-343522:biz:Dog Supplies:
Bessy McCarthur Jone2 (Dog (and cat) Supplies / 0438-343522)
=>name:Bessy McCarthur Jone2: phone:0438-343522:biz:Dog (and cat) Supplies:
Bessy (Bess, fails) McCarthur Jone3 (Dog Supplies / 0438-343522)
=>name:Bessy: phone:0438-343522:biz:Bess, fails) McCarthur Jone3 (Dog Supplies:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.