还是雨滴谱文件,这次尝试批量处理
首先处理1个单独的txt文件
#!usr/bin/env python # -*- coding:utf-8 _*- """ @author:Suyue @file:raindrop.py @time:2025/01/15 {DAY} @desc: """ import numpy as np import re def process_file(input_file, output_file): # 用于匹配时间戳的正则表达式(这里需要根据你的时间戳格式进行修改) timestamp_pattern = re.compile(r'^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$') current_array = [] timestamp = None results = [] with open(input_file, 'r') as f: for line in f: line = line.strip() # 检查是否是时间戳 if timestamp_pattern.match(line): # 如果当前数组不为空,说明我们完成了一个部分的读取 if current_array: # 将当前数组转换为numpy数组,并删除前2列和后9列 array = np.array(current_array, dtype=int) # 假设数据是浮点数 processed_array = array[:, 2:-9] # 删除前2列和后9列 # 如果没有超出边界,则保存结果 if processed_array.shape[1] > 0: results.append((timestamp, processed_array)) # 更新时间戳和当前数组 timestamp = line current_array = [] else: # 如果不是时间戳,则假设是数组数据(需要根据实际情况调整) # 这里假设数据是用空格分隔的浮点数 if line: # 避免处理空行 row = list(map(int, line.split())) current_array.append(row) # 检查最后一个数组(如果有的话) if current_array: array = np.array(current_array, dtype=int) processed_array = array[:, 2:-9] if processed_array.shape[1] > 0: results.append((timestamp, processed_array)) # 将结果保存到输出文件 with open(output_file, 'w') as out_f: for ts, array in results: # 写入时间戳(可以根据需要调整格式) out_f.write(f"{ts}\n") # 写入处理后的数组数据 for row in array: out_f.write(" ".join(map(str, row)) + "\n") out_f.write("\n") # 每个部分之间用空行分隔 # 使用示例 input_file = 'D:/rain/53464-20230627202000-20230627202159-0.txt' output_file = 'D:/rain/53464-20230627202000-20230627202159-1.txt' process_file(input_file, output_file)
标签:列和后,python,32,数组,current,file,timestamp,array,line From: https://www.cnblogs.com/shirleysu90/p/18671038