#!usr/bin/env python # -*- coding:utf-8 -*- """ @author: Suyue @file: speedeeinsert.py @time: 2024/04/16 @desc: """ # import numpy as np import pandas as pd df1 = pd.read_excel('G:/尺度速度.xls') file_path = 'G:/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 = [] for section in sections[1:]: # 将字符串拆分为几行,然后按空格拆分每行并转换为 DataFrame lines = section.strip().split('\n') matrix = [line.split() for line in lines] df = pd.DataFrame(matrix) # df.columns = df1['直径'] # df.index = df1['速度'] # 进行数浓度计算 n = df.iloc[0:33,2].values.astype(int) v = df1.iloc[0:33,1].values.astype(int) A = float(0.0054) t = float(60) # 循环读取ΔD d = df1['变化直径'] for i in d: ND = n / A * t * v * i ND1 = ND.sum() # print(ND1) print(ND1) # 显示每个dataframe形状以确认 # df_shapes = [df.shape for df in list] # print(df_shapes)
标签:拆分,df,df1,一种,算法,file,print,sections From: https://www.cnblogs.com/shirleysu90/p/18138354