Kwong 2019-11-06
# -*- coding: utf-8 -*-
1. 查找单个电话号码
from phone import Phone def get_phone_info(phone_num): phone_info = Phone().find(phone_num) try: phone = phone_info[‘phone‘] province = phone_info[‘province‘] #省 city = phone_info[‘city‘] #城市 zip_code = phone_info[‘zip_code‘] #邮编 area_code = phone_info[‘area_code‘] #区号 phone_type = phone_info[‘phone_type‘] #运营商 except: print(‘----- the phone number is empty, please check your phone number! -----‘) return phone, province, city, zip_code, area_code, phone_type if __name__ == "__main__": phone_num = ‘17713645840‘ #输入要查询的电话号码,注意只能是三大运营商的手机号码 if len(phone_num)==0: print(‘please input your phone number first!‘) phone_num = input("phone number:") phone_info = get_phone_info(phone_num) #列表返回 print(phone_info[0]) #电话号 print(phone_info[1]) #省份 print(phone_info[2]) #城市 print(phone_info[3]) #邮编 print(phone_info[4]) #区号 print(phone_info[5]) #运营商
2. 查找excel里面的电话号码
from phone import Phone import xlrd import os import xlsxwriter def get_phone_info(phone_num): phone_info = Phone().find(phone_num) try: phone = phone_info[‘phone‘] province = phone_info[‘province‘] #省 city = phone_info[‘city‘] #城市 zip_code = phone_info[‘zip_code‘] #邮编 area_code = phone_info[‘area_code‘] #区号 phone_type = phone_info[‘phone_type‘] #运营商 except: print(‘----- the phone number is empty, please check your phone number! -----‘) return phone, province, city, zip_code, area_code, phone_type def get_phone_info_excel(phone_number_path, sheet_num = 0): #解析地址 file_name = os.path.splitext(phone_number_path)[0] file_realname = file_name.split(‘\\‘)[-1] #文件名 print(file_realname) myWorkbook = xlsxwriter.Workbook(file_name+‘_get_info.xlsx‘)#创建一个excel文件,用于待会写数据 sheet_file_realname = myWorkbook.add_worksheet(file_realname)#在文件中创建一个名为file_realname的sheet,不加名字默认为sheet1 bold= myWorkbook.add_format({‘bold‘:True})#设置一个加粗的格式对象 # workfomat = workbook.add_format({ # ‘bold‘ : True, #字体加粗 # ‘border‘ : 1, #单元格边框宽度 # ‘align‘ : ‘center‘, #对齐方式 # ‘valign‘ : ‘vcenter‘, #字体对齐方式 # ‘fg_color‘ : ‘#F4B084‘, #单元格背景颜色 # }) sheet_file_realname.write(0, 0, u‘电话号‘,bold) #给第一行加标签 sheet_file_realname.write(0, 1, u‘省份‘,bold) sheet_file_realname.write(0, 2, u‘城市‘,bold) sheet_file_realname.write(0, 3, u‘邮编‘,bold) sheet_file_realname.write(0, 4, u‘区号‘,bold) sheet_file_realname.write(0, 5, u‘运营商‘,bold) #设置读Excel的编码格式 xlrd.Book.encoding = "gbk" rb = xlrd.open_workbook(phone_number_path) sheet = rb.sheet_by_index(sheet_num) #读第几个sheet,默认为第一个 nrows = sheet.nrows for i in range(nrows): phone_num = int(sheet.cell_value(i, 0)) #假设电话号码在第一列,从第一行开始,也就是索引(0,0), #int是因为读入的时候自动变成float,会超过电话号码长度 phone_info = get_phone_info(phone_num) sheet_file_realname.write(i + 1, 0, phone_info[0]) sheet_file_realname.write(i + 1, 1, phone_info[1]) sheet_file_realname.write(i + 1, 2, phone_info[2]) sheet_file_realname.write(i + 1, 3, phone_info[3]) sheet_file_realname.write(i + 1, 4, phone_info[4]) sheet_file_realname.write(i + 1, 5, phone_info[5]) myWorkbook.close() if __name__ == "__main__": phone_number_path = r‘D:\Python_workspace\spyder_space\电话号码.xlsx‘ get_phone_info_excel(phone_number_path)
3. 安装phone包:pip install phone