【问题标题】:Python genfromtxt issue reading in multiple tables from one datasetPython genfromtxt 问题从一个数据集中读取多个表
【发布时间】:2015-03-11 17:06:03
【问题描述】:

我正在尝试分别从所有这些数据表中读取数据以进行一些数据处理,但我似乎无法做到。

数据在这里: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)

【问题讨论】:

    标签: python genfromtxt


    【解决方案1】:

    这会读取列表中的 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-15
      • 1970-01-01
      • 1970-01-01
      • 2023-02-24
      • 2021-12-07
      • 2019-06-09
      • 2015-08-05
      • 2019-02-21
      相关资源
      最近更新 更多