# -*- coding:utf-8 -*- """ @author: 14931 @file: deletlie.py @time: 2024/05/01 @desc: """ import numpy as np import pandas as pd file_path = 'D:/NM004-20230627224400-20230627224859-0.txt' # 读整个txt文件读取到单个字符串 with open(file_path, 'r', errors='ignore') as file: file_content = file.read() # 按时间戳拆分内容以查找单独的部分 # 时间戳的格式为 YYYY-MM-DD HH:MM:SS,因此我们将使用正则表达式根据此模式进行拆分 import re sections = re.split(r'\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\n', file_content) # print(sections) # 如果txt第一个元素为空值(由于拆分),则将其删除 if not sections[0]: sections.pop(0) # 将每个部分放入列表 list = [] # 读取txt每组数据 for section in sections: # 将字符串拆分为几行,然后按空格拆分每行并转换为 DataFrame lines = section.strip().split('\n') matrix = [line.split() for line in lines] df = pd.DataFrame(matrix) # 删除前2列 df.drop(df.columns[:2],axis=1,inplace=True) # 删除后9列 df.drop(df.columns[-9:],axis=1,inplace=True) print(df) # df.to_csv('D:/output.txt',mode='a',index=False,header=False)
标签:file,df,代码,拆分,import,txt,sections From: https://www.cnblogs.com/shirleysu90/p/18169262