import re
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
import time
tic = time.time()
font = FontProperties(fname=r"C:\Windows\Fonts\SimHei.ttf", size=12)
# 打开文本文件
file_path = 'projectile.txt'
data_list = [] # 用于存储所有行的数据
with open(file_path, 'r') as file:
# 逐行读取文件内容
for line in file:
# 使用正则表达式查找科学计数法表示的数据
scientific_notations = re.findall(r'[-+]?\d*\.\d+[eE][-+]?\d+|\d+[eE][-+]?\d+', line)
# 如果在某一行中找到了匹配项,将匹配到的科学计数法表示的数据存入列表
if scientific_notations:
data_list.append([float(value) for value in scientific_notations])
# 将列表转换为二维数组
data_array = np.array(data_list)
# 用于记录整数倍出现的次数的字典
count_dict = {}
# 用于存储符合要求的行的新数组
filtered_array = []
# 遍历每一行
for row in data_array:
# 获取第一个数据
first_value = row[0]
# 检查第一个数据是否为1e-7的整数倍
if first_value % 1e-7 == 0:
# 记录整数倍出现的次数
count_dict[first_value] = count_dict.get(first_value, 0) + 1
# 如果整数倍出现的次数为1,则将该行添加到新数组中
if count_dict[first_value] == 1:
filtered_array.append(row)
# 转换为 NumPy 数组
filtered_array = np.array(filtered_array)
# 打印符合要求的行
print("符合要求的行:")
print(filtered_array)
toc = time.time()
duration = toc - tic
print(f"代码运行时长:{duration} 秒")
python代码 处理 fluent 产生的 txt 文件
最新推荐文章于 2024-08-15 01:55:01 发布