0

每次我想使用不同的名称时,我都想使用 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...我该怎么办?此致

4

2 回答 2

1

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

df.to_csv(filecsv_name + '.csv') 
于 2020-11-30T20:38:20.780 回答
1

我不知道为什么管理员会以某种方式行事,给我反对票并阻止我提问。但是,我真的很感谢这个论坛的用处和大家的时间。正如我的问题中经常发生的那样,如果我找到解决方案,我也会发布它。此外,我尝试询问我们个人和每个人(尤其是每个人)都有的事情。也就是说,我发布了解决方案。

请记住,定义 columnNames 是为了指定标题和数据。

print(' Save time history data for time dependent profile as filename)')
print('-------------------------------------------------------- ')

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

print(filename)

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

df.to_csv(filename+ '.csv')

一如既往地感谢所有人并感谢stackoverflow

于 2020-12-01T15:47:39.093 回答