fluent数据处理+python

文章介绍了使用Python进行数据处理的方法,包括删除文件中的行、读取并排序文件夹中的文件,以及计算雷诺数、温度差、传热系数和Nusselt数等,展示了在IT技术中处理和分析数据的基本步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

data数据处理

# -*- coding:utf-8 -*-
"""
作者:Huang jin
日期:2022年07月10日
"""

import numpy as np

def delete_first_lines(filename, count):
    # 打开文件并读取所有行
    with open(filename, 'r') as fin:
        lines = fin.readlines()
    
    # 将除了前count行外的内容写回文件
    with open(filename, 'w') as fout:
        fout.writelines(lines[count:])

# 调用函数删除文件前count行
filename = 'D:/1.data'
count = 24
delete_first_lines(filename, count)

# 读取文件内容并转换为numpy数组
with open('D:/1.data', 'r') as file:
    line = file.read()
    a = np.array(line.split())

# 计算数组长度并重新形成矩阵
b = int(len(a) / 12)
c = a.reshape(b, 12)

# 提取第7列数据并计算平方
d = c[:, 6]
h = []
for i in range(len(d)):
    e = float(d[i]) ** 2
    h.append(e)

# 计算平方数的平均值的平方根
h = np.array(h)
ave = (np.sum(h) / len(h)) ** 0.5

按顺序读取文件

# -*- coding:utf-8 -*-
"""
作者:Huang jin
日期:2022年06月26日
"""

import os

# 读取并排序文件夹中的文件名
def read_order(path):
    path_list = os.listdir(path)
    path_list.sort(key=lambda x: int(x[:-4]))
    return path_list

# 调用函数读取文件名
path = 'D:/re140000/data'
file_list = read_order(path)
print(file_list)

计算

# -*- coding:utf-8 -*-
"""
作者:Huang jin
日期:2022年06月25日
"""

import numpy as np
import matplotlib.pyplot as plt

# 定义变量
mu = 1.7894e-5  # 粘度,单位Pa·s
rho = 1.225  # 空气密度,单位kg/m3
d = 0.1  # 球直径,单位m
Re = np.array([4200, 70800, 101300, 140000, 170000, 186000, 219000]) # 雷诺数

# 计算速度
v = Re * mu / rho / d
print('v=', v)

# 定义圆弧
pi = np.pi
r = 50  # 半径,单位mm
sita = np.arange(0.05 * pi, 1.05 * pi, 0.05 * pi)

# 计算圆弧上的坐标
x = r * np.cos(sita)
y = r * np.sin(sita)

# 定义温度数据
T1 = np.array(
    [303.333543, 303.3081298, 303.502642, 303.8113138, 304.3036426, 305.1254775, 306.1010431, 307.4044065, 309.1425538,
     311.6437288, 315.4106954, 322.2443509, 344.5916522, 870.762207, 976.1358575, 417.1701027, 705.9871091, 561.6407437,
     446.3111629, 423.7783842])
T2 = np.array(
    [300.2213342, 300.2046029, 300.3479636, 300.5716531, 300.9255654, 301.5132523, 302.21561, 303.1578445,
     304.4208167, 306.2786451, 309.1318981, 314.4921014, 333.7540381, 936.1679611, 746.6937354, 369.4436744,
     676.5615191, 535.0384478, 428.2147555, 411.3469609])

# 计算温度差和传热系数
deltaT1 = T1 - 293
deltaT2 = T2 - 293
h1 = 25000 / deltaT1
h2 = 25000 / deltaT2

# 绘图
plt.plot(sita, h1, 'g')
plt.plot(sita, h2, 'r')
plt.show()

删除前三行

# -*- coding:utf-8 -*-
"""
作者:Huang jin
日期:2022年06月26日
"""

import os

# 重命名文件
def rename_files(path, substr, new_substr):
    files = os.listdir(path)
    for file in files:
        old = os.path.join(path, file)
        new = os.path.join(path, file.replace(substr, new_substr))
        os.rename(old, new)

# 删除文件前count行
def delete_first_lines(filename, count):
    with open(filename, 'r') as fin:
        lines = fin.readlines()
    
    with open(filename, 'w') as fout:
        fout.writelines(lines[count:])

# 读取并排序文件夹中的文件名
def read_order(path):
    path_list = os.listdir(path)
    path_list.sort(key=lambda x: int(x[:-4]))
    return path_list

path = 'D:/re140000/data'

# 重命名文件
rename_files(path, 'rfile', '')

# 重命名文件
rename_files(path, 'a0', '')

# 重命名文件
rename_files(path, '-', '')

# 获取文件列表并删除前3行
file_list = read_order(path)
for file in file_list:
    filename = os.path.join(path, file)
    delete_first_lines(filename, 3)

数据处理

# -*- coding:utf-8 -*-
"""
作者:Huang jin
日期:2022年06月26日
"""

import numpy as np
import matplotlib.pyplot as plt

# 读取out文件并转化为数组
def read_data(filename):
    with open(filename, 'r') as file:
        line = file.read()
        data = np.array(line.split()).astype(float)
        data = data.reshape(-1, 2)
    return data

# 读取所有out文件数据
file_list = ['D:/re140000/data/{}.out'.format(i) for i in range(21)]
data_list = [read_data(file) for file in file_list]

# 提取温度数据并计算平均值
temperature_list = [data[2500:2600, 1] for data in data_list]
average_temperature_list = [np.mean(temperature) for temperature in temperature_list]

# 计算温度差、传热系数和Nusselt数
delta_T = np.array(average_temperature_list) - 293
h = 25000 / delta_T
Nu = h * 0.1 / 0.0242

# 绘制图像
sita = np.arange(0, 1.05 * np.pi, 0.05 * np.pi)
plt.plot(sita, Nu, 'g')
plt.show()

文件名批量修改

# -*- coding:utf-8 -*-
"""
作者:Huang jin
日期:2022年07月04日
"""

import os

# 输入文件夹地址
path = "D:/re140000/data"
files = os.listdir(path)

# 输出所有文件名,只是为了看一下
for file in files:
    print(file)

# 获取旧名和新名并重命名文件
i = 0
for file in files:
    # old 旧名称的信息
    old = os.path.join(path, files[i])

    # new是新名称的信息,这里的操作是删除了最前面的8个字符
    new = os.path.join(path, files[i].replace('rfile', ''))
    new = os.path.join(path, files[i].replace('a0', ''))
    new = os.path.join(path, files[i].replace('-', ''))

    # 新旧替换
    os.rename(old, new)
    i += 1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿斯弗的撒旦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值