python3 提取文本中的手机号以及其他数字

# 目标:需要提取文本中的手机号和其他所有数字
# 文本: QQ123456扣扣562523112
# 结果:[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

你可能感兴趣的:(Python)