gpt4 book ai didi

python - 选择名称以保存 .csv 文件并使用在整个 python 脚本中输入的名称

转载 作者:行者123 更新时间:2023-12-04 18:59:38 24 4
gpt4 key购买 nike

每次我想使用不同的名称时,我都想使用 Python 保存一个 .csv 文件。所以我写了这段代码。除此之外,我想在保存后继续运行的脚本的其余部分中使用新名称。我怎样才能做到这一点?建议?非常感谢。

from __future__ import division
from __future__ import print_function
from bigfloat import *
from builtins import *
from decimal import*
from math import*
import numpy as np
import time


# Initialize Cantera for numerical simulation

import pandas as pd
from pandas import DataFrame
import numpy as np
import time
import cantera as ct

print("Running Cantera version: {}".format(ct.__version__))
print("")

import matplotlib.pyplot as plt

plt.style.use('ggplot')
plt.style.use('seaborn-pastel')

plt.rcParams['axes.labelsize'] = 18
plt.rcParams['xtick.labelsize'] = 14
plt.rcParams['ytick.labelsize'] = 14
plt.rcParams['figure.autolayout'] = True



#################################################################

phi=1.2

Ti=900 # Kelvin

Pi=1.2 # Atm

#################################################################

# Load the kinetic mechanism for

gas = ct.Solution('tester.cti')

# Inlet gas conditions
reactorTemperature = Ti #row[5] # Kelvin
reactorPressure = Pi*ct.one_atm # in atm
gas.set_equivalence_ratio(phi, 'H2:3', 'O2:3, N2:94, N2O:0.00005')
gas.TP= reactorTemperature, reactorPressure

# Mean Molecular Weight

MR=gas.mean_molecular_weight

print('')
print('Mean molecular weight for gas declared :', float(format(MR, '.3f')))
print('')

# Reactor parameters
residenceTime = 0.21 # s
reactorVolume = 113*(1e-2)**3 # m3

# Instrument parameters

# This is the "conductance" of the pressure valve and will determine its efficiency in
# holding the reactor pressure to the desired conditions.
pressureValveCoefficient = 0.001

# This parameter will allow you to decide if the valve's conductance is acceptable. If there
# is a pressure rise in the reactor beyond this tolerance, you will get a warning
maxPressureRiseAllowed = 0.01

# Simulation termination criterion
maxSimulationTime = 5 # seconds


# Definition of reservoirs
fuelAirMixtureTank = ct.Reservoir(gas)
exhaust = ct.Reservoir(gas)
env = ct.Reservoir(gas)

# Definition of Reactor
stirredReactor = ct.IdealGasReactor(gas, energy='on', volume=reactorVolume)

# Definition of an heat wall exchange
w=ct.Wall(stirredReactor, env, A=0.0113, U=220)

# Declaration of controllers

massFlowController = ct.MassFlowController(upstream=fuelAirMixtureTank,
downstream=stirredReactor,
mdot=MR*reactorPressure*reactorVolume/ct.gas_constant/gas.T/residenceTime)


pressureRegulator = ct.Valve(upstream=stirredReactor,
downstream=exhaust,
K=pressureValveCoefficient)

# Definition of reactorNetwork solver
reactorNetwork = ct.ReactorNet([stirredReactor])

# Now compile a list of all variables for which we will store data
columnNames = [stirredReactor.component_name(item) for item in range(stirredReactor.n_vars)]
columnNames = ['pressure'] + columnNames




filename = input('filename: ')
filename = str(filename)

print(filename)

df = pd.DataFrame(filename, columns= columnNames)

df.to_csv (eval(filename))
我想将数据保存在 .csv 文件中,其中定义了 ColumnNames...我该怎么办?
此致

最佳答案

如果您使用的是 pandas,您可以通过以下方式保存为 csv:

df.to_csv(filecsv_name + '.csv') 

关于python - 选择名称以保存 .csv 文件并使用在整个 python 脚本中输入的名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65080739/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com