粒径的取值范围为:0.31~8mm
因此excel中标记红色的都需要删除:
txt文件为(红框为留下来的数据),一共五组数,也就是五个时间的数:
那么我只留下我需要的d的n的数据,删除不需要的列:
# -*- coding:utf-8 -*- """ @author: su @file: deletlie.py @time: 2024/05/01 @desc: """ import numpy as np import pandas as pd file_path = '/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) date = [x for x in file_content.split('\n') if len(x) == 19] # 如果txt第一个元素为空值(由于拆分),则将其删除 if not sections[0]: sections.pop(0) final_data = {} for i in range(len(sections)): final_data[date[i]] = sections[i] df_final_values = [] for key, value in final_data.items(): lines = value.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) df_final_values.append(df.values.tolist()) index = 0 with open('/output.txt', 'w', errors='ignore') as file: for key, _ in final_data.items(): file.write(key + '\n') for df_values in df_final_values[index]: file.write('\t'.join(df_values) + '\n') index += 1 file.write('\n')
得到:
Δd为当前d-前一个d,剔除不要的红色值得到:
标签:处理,雨滴,df,values,关于,file,txt,sections,final From: https://www.cnblogs.com/shirleysu90/p/18169492