import re import xlwt import time wb = xlwt.Workbook() # 創建excel文件,設置編碼,(注意這里的Workbook首字母是大寫) table = wb.add_sheet(u'datasheet1', cell_overwrite_ok=True) # 通常設置為True,這個參數的意義在於,如果對一個單元格重復操作 with open(r'E:\Download\tc\0D9F7B3CGG.dat', encoding='gb18030') as f: lines = f.readlines() len = len(lines) funcid = [] for i in range(0, len): # range防止下標越界 try: a = lines[i][132:] res = re.findall(r'\d{6}', a) print("Functionid=", res[0]) print("i===", i) table.write(i, 1, res[0])#如果是寫入中文,則要用u'漢字'的形式。比如 except: print("異常信息") # time1 = time.strftime('%Y%m%d%H%M%S', time.localtime(time.time())) wb.save("服務CESHIXIANGG1" + time1 + ".xls")
思路:讀取dat文件,返回類型為列表類型,截取列表每行指定的數據,並存入excel