Python genfromtxt问题从一个数据读入多个表

2024-10-03 15:25:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从所有这些数据表中分别读取一些数据处理,但我似乎无法做到这一点。你知道吗

数据可在这里获得:http://cdsweb.u-strasbg.fr/topbase/tables/AGS05.OP17

有126个表,它给了我前100个表的空数组,然后逐渐填充它们。我做错什么了?你知道吗

代码:

import numpy as np
from matplotlib import pyplot as plt

for x in np.arange(0,126):
    table = np.genfromtxt('AGS05.OP17',skip_header=(245+(x*71)),skip_footer=(9500-(x*71)))
    print x
    print table

另外,如果我只想得到第一个

In [38]: np.genfromtxt('AGS05.OP17',skip_header=245, skip_footer=9500)
Out[38]: array([], dtype=float64)

Tags: 数据importhttpasnptable数据处理数据表
1条回答
网友
1楼 · 发布于 2024-10-03 15:25:39

这将读取列表中的126个表:

tables = []
diff_header = 76
diff_footer = 73
with open('AGS05.OP17', 'rb') as fobj:
    for x in np.arange(126):
        header_skip = 245 + x * diff_header
        footer_skip = 9125 - x * diff_footer
        tables.append(np.genfromtxt(fobj, skip_header=header_skip,
                                    skip_footer=footer_skip))
        fobj.seek(0)

>>> len(tables)
126
>>> len(tables[0])
70
>>> len(tables[-1])
>>> tables[-1]
array([[ 3.75 , -0.433, -0.838, ...,  0.162,  0.592,  0.986],
       [ 3.8  ,  9.999, -0.476, ...,  0.372,  0.797,  1.227],
       [ 3.85 ,  9.999, -0.388, ...,  0.765,  1.094,  1.467],
       ..., 
       [ 8.3  ,  9.999,  9.999, ...,  9.999,  9.999,  9.999],
       [ 8.5  ,  9.999,  9.999, ...,  9.999,  9.999,  9.999],
       [ 8.7  ,  9.999,  9.999, ...,  9.999,  9.999,  9.999]])
70

相关问题 更多 >