简体   繁体   中英

how does the re.finditer loop work?

I am trying to fetch email, whatzapp, viber from an instagram API. I store the downloaded data into a variable called text and check for patterns of email, watzapp, viber numbers ect.

Sample texts:=

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

The loop is this:-

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')

The problem is when it does not find any of the data which i need ie email id's , watzapp, line, viber numbers ect, I have given in the loop that the lists should store N/A. Here data1,data2 ect are the python list variables.

how to make the lists store "N/A" when the pattern is not matched?

You need to add a check if there is a match before running the re.finditer :

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

Then, when adding N/A to the resulting lists, check if the value is already present in the list:

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

Here is 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM