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

  

posted @ 2022-03-01 17:02  木棉花的漂泊  阅读(2620)  评论(0编辑  收藏  举报