python之讀取dat文件,並取對應字符串


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

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM