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

标签 python csv ubuntu cantera

每次我想使用不同的名称时,我都想使用 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/

相关文章:

python - 使用中间结果的嵌套列表理解

python - 知道如何将其读取为 CSV 吗? - Python

python - 使用Python将csv加载到mysql中语法错误

php - 如何在 ubuntu 15 上安装 pcntl 扩展

python - Numpy 高级选择不起作用

python - seaborn FacetGrid 图中时间数据的设置

java - 在用 Ant 编译的文件中找不到主类

c++ - gets() 未在范围内声明

c++ - 如果存在打开的文件 fd,inotify 不会引发 DELETE_SELF

python - 如何为 numpy 数组制作非方形滚动窗口?