Python 2.7 如何在Python中读取Fortran固定宽度格式的文本文件?
我有一个Fortran格式的文本文件(前三行): 以及文件格式说明:Python 2.7 如何在Python中读取Fortran固定宽度格式的文本文件?,python-2.7,fortran,ascii,Python 2.7,Fortran,Ascii,我有一个Fortran格式的文本文件(前三行): 以及文件格式说明: -------------------------------------------------------------------------------- Bytes Format Units Label Explanations -------------------------------------------------------------------------------- 1- 10 A
--------------------------------------------------------------------------------
Bytes Format Units Label Explanations
--------------------------------------------------------------------------------
1- 10 A10 --- WDS WDS(J2000)
12- 14 A3 --- Primary Designation of the primary
16- 18 A3 --- Secondary Designation of the secondary component
20- 22 A3 --- Parent Designation of the parent (1)
24- 29 A6 --- Type Observing technique/status (2)
31- 35 F5.2 d logP ? Logarithm (10) of period in days
37- 44 F8.3 --- Sep Separation or axis
45 A1 --- x_Sep ['"m] Units of sep. (',",m)
47- 49 I3 deg PA Position angle
51- 55 F5.2 mag Vmag1 V-magnitude of the primary
57- 61 A5 --- SP1 Spectral type of the primary
63- 67 F5.2 mag Vmag2 V-magnitude of the secondary
69- 73 A5 --- SP2 Spectral type of the secondary
75- 79 F5.2 solMass Mass1 Mass of the primary
80 A1 --- MCode1 Mass estimation code for primary (3)
82- 86 F5.2 solMass Mass2 Mass of the secondary
87 A1 --- MCode2 Mass estimation code for secondary (3)
89-108 A20 --- Rem Remark
如何用Python读取我的文件。我只找到了库中的函数
这是唯一可行和有效的方法吗?我希望我能做到这一点没有。你有什么建议吗
columns = ((0,10),(11,14),(15,18),(19,22),(23,29),(30,35),
(36,44),(44,45),(46,49),(50,55),(56,61),(62,67),
(68,73),(74,79),(79,80),(81,86),(86,87),(88,108))
string=file.readline()
dataline = [ string[c[0]:c[1]] for c in columns ]
注意,列索引为(起始字节-1,结束字节),因此可以使用单个字符字段
例:(44,45)
这将为您留下一个字符串列表。您可能希望转换为浮点、整数等。关于这个主题,这里有许多问题。有一个模块FortranRecordReader,但它在现代fortran文件包含的星号、注释等方面很弱。不过,对于一个好的文件,它与namedtuple结合使用还是很有用的。例如:
from fortranformat import FortranRecordReader
fline=FortranRecordReader('(a1,i3,i5,i5,i5,1x,a3,a4,1x,f13.5,f11.5,f11.3,f9.3,1x,a2,f11.3,f9.3,1x,i3,1x,f12.5,f11.5)')
from collections import namedtuple
record=namedtuple('nucleo','cc NZ N Z A el o massexcess uncmassex binding uncbind B beta uncbeta am_int am_float uncatmass')
f=open('AME2012.mas12.ff','r')
for line in f:
nucl=record._make(fline.read(line))
您也可以尝试“解析”模块,或编写您的文件。这种类型的文件可以通过astropy表格读取。您显示的标题看起来很像CDS格式的ascii表,它有一个特定的读卡器:
到目前为止,您尝试了什么?你能给我们看一些代码吗…我试过pandas read_fwf函数。它可以工作,但我不想在我的程序中使用额外的模块。例如,我想用NumPy解决我的任务。你能给我们看一些代码吗?单列索引应该是例如
(44,45)
。否则,它们将返回一个空列表。
columns = ((0,10),(11,14),(15,18),(19,22),(23,29),(30,35),
(36,44),(44,45),(46,49),(50,55),(56,61),(62,67),
(68,73),(74,79),(79,80),(81,86),(86,87),(88,108))
string=file.readline()
dataline = [ string[c[0]:c[1]] for c in columns ]
from fortranformat import FortranRecordReader
fline=FortranRecordReader('(a1,i3,i5,i5,i5,1x,a3,a4,1x,f13.5,f11.5,f11.3,f9.3,1x,a2,f11.3,f9.3,1x,i3,1x,f12.5,f11.5)')
from collections import namedtuple
record=namedtuple('nucleo','cc NZ N Z A el o massexcess uncmassex binding uncbind B beta uncbeta am_int am_float uncatmass')
f=open('AME2012.mas12.ff','r')
for line in f:
nucl=record._make(fline.read(line))