re.finditer循环如何工作?

时间:2016-05-11 10:21:15

标签: python regex

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

示例文本:=

Recruitment AgentsThe most powerful manufacturers,we have thebest quality.Wechat:13255996580Whatsapp:+8618820784535

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

Bags Manufacturer \n✈️ ship worldwide \nTake 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"当模式不匹配时?

1 个答案:

答案 0 :(得分:1)

在运行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\nD&G,Chanel,Hermes,Miu Miu,Balmain,Dior\nDelivery 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)