python代码 处理 fluent 产生的 txt 文件

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} 秒")

  • 5
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值