# 目标:需要提取文本中的手机号和其他所有数字 # 文本: QQ:123456扣扣5625231和12 # 结果:[123456],[5625231],[12] import re import os # 需要处理的文本 filename1 = r'E:\.txt' mobileblack = r"E:\.txt" qqblack = r"E:\.txt" if os.path.exists(mobileblack): os.remove(mobileblack) if os.path.exists(qqblack): os.remove(qqblack) f1 = open(filename1, 'r', encoding='utf-8') def strreplace(stra): str1 = stra.replace("一","1") str2 = str1.replace("二", "2") str3 = str2.replace("三", "3") str4 = str3.replace("四", "4") str5 = str4.replace("五", "5") str6 = str5.replace("六", "6") str7 = str6.replace("七", "7") str8 = str7.replace("八", "8") str9 = str8.replace("九", "9") str10 = str9.replace("壹", "1") str11 = str10.replace("贰", "2") str12 = str11.replace("叁", "3") str13 = str12.replace("肆", "4") str14 = str13.replace("伍", "5") str15 = str14.replace("陆", "6") str16 = str15.replace("柒", "7") str17 = str16.replace("捌", "8") str18 = str17.replace("玖", "9") str19 = str18.replace("幺", "1") str20 = str19.replace("零", "0") str21 = str20.replace("①", "1") str22 = str21.replace("②", "2") str23 = str22.replace("③", "3") str24 = str23.replace("④", "4") str25 = str24.replace("⑤", "5") str26 = str25.replace("⑥", "6") str27 = str26.replace("⑦", "7") str28 = str27.replace("⑧", "8") string = str28.replace("⑨", "9") return string def writemobile(item): f = open(mobileblack, 'a', encoding='utf-8') f.write(item) f.write("\n") f.close() def writeQQ(item): f = open(qqblack, 'a', encoding='utf-8') f.write(item) f.write("\n") f.close() reader1 = f1.readlines() for line in reader1: stra = re.sub("[\s+\.\!\/><_,$‘’·`%::�♦^♥❤*.(+\"\']+|[+——!,。?、~@br#¥%……&*()]+|[-]", "", line) string = strreplace(stra) pattern = re.compile(u"1[34578]\d{9}") phone = pattern.findall(string) if phone: writemobile(str(phone)) try: mode = re.compile(r'\d+') qq = mode.findall(string) if qq: writeQQ(str(qq)) except Exception as e: print(e) print('空值') pass