Python筛选Excel数据,读取、保存

#coding:utf-8https://mp.csdn.net/postedit
import re
import xlrd
import xlwt
def main():
    #读取Excel
    wk=xlrd.open_workbook(r'C:\Users\https://mp.csdn.net/posteditPublic\Nwt\cache\recv\旧白名单.xlsx')
    #获取目标EXCEL文件sheet名
    sheets=wk.sheet_names()
    #根据表索引
    ws=wk.sheet_by_index(0)
    #获取总行数
    nrows=ws.nrows
    #获取总列数
    ncols=ws.ncols
    print(nrows,ncols)
    #获取一列的值row_values(i)#获取一行的数值
    datarow=ws.col_values(1)
    # print(type(datarow))
    #拼接字符串
    text=" ".join('%s' %id for id in datarow)
    # print(type(text))
    m=re.findall(r"130\d{8}",text)
    if m:
        print('1111',m)
        return m

def main_n(fun):
    m=fun()
    print("5555",m)
    book = xlwt.Workbook(encoding='utf-8', style_compression=0)
    sheet = book.add_sheet('demom', cell_overwrite_ok=True)
    count=0
    for a in m:
        count+=1
        print(count)
        sheet.write(count,0,a)
        book.save('130.xls')
if __name__ == "__main__":
    main_n(main)

你可能感兴趣的:(Python第三方库)