python之读取dat文件,并取对应字符串

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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")

  

1
思路:读取dat文件,返回类型为列表类型,截取列表每行指定的数据,并存入excel

  

posted @   木棉花的漂泊  阅读(2705)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
· AI 智能体引爆开源社区「GitHub 热点速览」
点击右上角即可分享
微信分享提示