我有一個字串:
string= "**Started:** 2021-07-04 11:51:31 PM BST | **Finished:** 2021-07-04 11:51:46
PM BST | **Duration:** 1 Minute
---
Company| Participant| Email | Joined| Duration| Messages
---|---|---|---|---|---
global merchant Bank (GR) ((PM) by TR) (Disclaimer)| Bokng Kim|
[email protected]| 2021-07-04 11:51:31 PM BST| 1 Minute | 0
Brokers LP (GR) ((PM) by TR) (KW)| Ren Kim| [email protected]|
2021-07-04 11:51:31 PM BST| 1 Minute | 2
---"
我想從中提取姓名和電子郵件 ID,即,
names=['Bokng Kim','Ren Kim']
email=['[email protected]','[email protected]']
uj5u.com熱心網友回復:
這是一個正則運算式re.findall選項。首先,我們在列標題上拆分輸入文本,留下包含實際內容的文本。然后,我們做一個正則運算式查找所有針對第二和第三個管道分隔的列。
string = """**Started:** 2021-07-04 11:51:31 PM BST | **Finished:** 2021-07-04 11:51:46
PM BST | **Duration:** 1 Minute
---
Company| Participant| Email | Joined| Duration| Messages
---|---|---|---|---|---
global merchant Bank (GR) ((PM) by TR) (Disclaimer)| Bokng Kim|
[email protected]| 2021-07-04 11:51:31 PM BST| 1 Minute | 0
Brokers LP (GR) ((PM) by TR) (KW)| Ren Kim| [email protected]|
2021-07-04 11:51:31 PM BST| 1 Minute | 2
---"""
inp = string.split('---|---|---|---|---|---')[1]
matches = re.findall(r'.*?\|\s*(.*?)\s*\|\s*(.*?)\s*\|', inp)
names = [x[0] for x in matches]
email = [x[1] for x in matches]
print(names) # ['Bokng Kim', 'Ren Kim']
print(email) # ['[email protected]', '[email protected]']
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/402982.html
標籤:
