gpt4 book ai didi

python - re.finditer 循环是如何工作的?

转载 作者:太空宇宙 更新时间:2023-11-04 05:30:54 26 4
gpt4 key购买 nike

我正在尝试从 Instagram API 获取电子邮件、whatzapp、viber。我将下载的数据存储到一个名为 text 的变量中,并检查电子邮件、watzapp、viber 号码等的模式。

示例文本:=

Recruitment Agents👑👑👑👑The most powerful manufacturers,we have thebest quality.📱Wechat:13255996580💜📱Whatsapp:+8618820784535

🌸 เข้าช้อปทุกวันจ้า🌸 ซื้อกับวี้ได้ของแท้แน่นอนค่า🌸 แบรนด์อื่นสอบถามได้ค่า🌸 ดรีวิว@reviewkayasisshopp🌸 LINE ID : @kux1427k (มี @ ด้วยจ้า)

Bags Manufacturer \n✈️ ship worldwide \n💰Take PayPal/WU\n☎️ phone/whats app: +8613025173183\n☎️ we/chat:2268633046

循环是这样的:-

text=init['bio']
pattern=r'(?i)([\w.]+@[\w.]+)|(?:(?:\b|[,/]\s*)(?:whatsapp|whats app|viber|wechat|we/chat))+\b\s*[::]?\s*([‌​()+\d -]+)|\bline(?:\sid)?\s*(?:[::]\s*)?(@\w+)|((?:\+\d+)?[ -]?(?:\(\d+\)[ -]?)?[\d -]{6,}\d)'
for mobj in re.finditer(pattern,text):
if mobj.group(1):
data1.append(mobj.group(1))
else:
data1.append('N/A')
if mobj.group(2):
t= mobj.group().lower()
if 'whatsapp' in t:
data2.append(mobj.group(2))
if 'viber' in t:
data3.append(mobj.group(2))
if 'wechat' in t:
data4.append(mobj.group(2))
if 'whats app' in t:
data2.append(mobj.group(2))
if 'we/chat' in t:
data4.append(mobj.group(2))
else:
data2.append('N/A')
data3.append('N/A')
data4.append('N/A')
if mobj.group(3):
data5.append(mobj.group(3))
else:
data5.append('N/A')
if mobj.group(4):
data6.append(mobj.group(4))
print(data6)
else:
data6.append('N/A')

问题是当它找不到我需要的任何数据时,即电子邮件 ID、watzapp、线路、viber 号码等,我在循环中给出了列表应该存储 N/A。这里 data1,data2 等是 python 列表变量。

当模式不匹配时,如何使列表存储“N/A”?

最佳答案

在运行 re.finditer 之前,您需要添加一个检查是否存在匹配项:

if re.search(pattern, text):
# go on with finditer

然后,在将 N/A 添加到结果列表时,检查该值是否已存在于列表中:

if 'N/A' not in data1:
data1.append('N/A')

这是一个demo :

import re
data1 = []
data2 = []
data3 = []
data4 = []
data5 = []
data6 = []
text="👛The most affordable prices\n👗D&G,Chanel,Hermes,Miu Miu,Balmain,Dior\n📦Delivery 2 weeks\n✈Woldwide shipping\n📞+16166350689; +998909729723"
pattern=r'(?i)([\w.]+@[\w.]+)|(?:(?:\b|[,/]\s*)(?:whatsapp|whats app|viber|wechat|we/chat))+\b\s*[::]?\s*([()+\d -]+)|\bline(?:\sid)?\s*(?:[::]\s*)?(@\w+)|((?:\+\d+)?[ -]?(?:\(\d+\)[ -]?)?[\d -]{6,}\d)'
if re.search(pattern, text):
for mobj in re.finditer(pattern,text):
if mobj.group(1):
data1.append(mobj.group(1))
else:
if 'N/A' not in data1:
data1.append('N/A')
if mobj.group(2):
t= mobj.group().lower()
if 'whatsapp' in t:
data2.append(mobj.group(2))
if 'viber' in t:
data3.append(mobj.group(2))
if 'wechat' in t:
data4.append(mobj.group(2))
if 'whats app' in t:
data2.append(mobj.group(2))
if 'we/chat' in t:
data4.append(mobj.group(2))
else:
if 'N/A' not in data2:
data2.append('N/A')
if 'N/A' not in data3:
data3.append('N/A')
if 'N/A' not in data4:
data4.append('N/A')
if mobj.group(3):
data5.append(mobj.group(3))
else:
if 'N/A' not in data5:
data5.append('N/A')
if mobj.group(4):
data6.append(mobj.group(4))
print(data6)
else:
if 'N/A' not in data6:
data6.append('N/A')
print(data1)
print(data2)
print(data3)
print(data4)
print(data5)
print(data6)

关于python - re.finditer 循环是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37159737/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com