需要帮助使用ascii来编写.dat文件

问题描述 投票:-1回答:1

目前,我正在尝试在python anaconda中使用astropy.io.ascii来编写一个.dat文件,其中包含我已经从不同的ascii文件中读取的数据(使用.dat)。我在预先存在的文件中将一个特定的表定义为Data,Data的问题是我需要将第一列乘以因子101325来改变它的单位,我需要将四列中的第四列消失完全。所以我将第一列定义为Pressure_pa并且我转换了它的单位,然后我将其他两列定义为Altitude_kmTemperature_K。有什么方法我可以使用asciiwrite函数告诉它写一个包含我定义的三列的.dat文件?我该怎么办呢?下面的代码使我已经定义了这三列数据:

from astropy.io import ascii
Data=ascii.read('output_couple_121_100.dat',guess=False,header_start=384,data_start=385,data_end=485,delimiter=' ')
Pressure_pa=Data['P(atm)'][:}*101325
Altitude_km=Data['Alt(km)'][:]
Temperature_K=Data['T'][:]

现在我想我可以使用ascii.write(),用.datPressure_paAltitude_km写一个Temperature_K文件到同一个文件中,有什么方法可以做到这一点吗?

python ascii astropy
1个回答
1
投票

所以我想我想通了!我将创建一个更通用的版本以适应其他人

    from astropy.io import ascii
    Data=ascii.read('filename.dat',guess=False,header_start=1,data_start=2,data_end=10,delimiter=' ')
    #above: defining Data as a certain section of a .dat file beginning at line 2 through 10 with headers in line 1
    ascii.write(Data,'new_desired_file_name.dat',names=['col1','col2','col3','col4'],exclude_names=['col3'],delimiter=' ')
    #above: telling ascii to take Data and creat a .dat file with it, when defining the names, define a name for every column in Data and then use the exclude_names command to tell it not to include those specific columns
© www.soinside.com 2019 - 2024. All rights reserved.