python - 从文件头获取特定元素?

标签 python python-2.7

所以我有这种格式的数据:

#NUMBER OF AGES=37 MAGS= 5
#----------------------------------------------------       
#MIX-LEN  Y      Z          Zeff        [Fe/H] [a/Fe]
# 1.9380  0.3300 3.1125E-04 1.5843E-04  -1.99   0.40
#----------------------------------------------------       
#**PHOTOMETRIC SYSTEM**: SDSS (AB)
#----------------------------------------------------       
#AGE= 1.000 EEPS=276
#EEP   M/Mo    LogTeff  LogG   LogL/Lo sdss_u  sdss_g  sdss_r  sdss_i  sdss_z  
   3  0.105051  3.5416  5.3073 -2.7281 17.2434 13.9245 12.2511 11.5059 11.0920
   4  0.118159  3.5519  5.2644 -2.5931 16.5471 13.4392 11.8286 11.1502 10.7665
   5  0.133632  3.5623  5.2211 -2.4547 15.8825 12.9522 11.4091 10.7861 10.4301
   6  0.156724  3.5737  5.1745 -2.2933 15.1633 12.4008 10.9372 10.3638 10.0348
   7  0.194206  3.5861  5.1259 -2.1020 14.3673 11.7681 10.3975  9.8690  9.5654

#AGE= 1.250 EEPS=275
#EEP   M/Mo    LogTeff  LogG   LogL/Lo sdss_u  sdss_g  sdss_r  sdss_i  sdss_z  
   4  0.105349  3.5419  5.3062 -2.7248 17.2255 13.9125 12.2406 11.4972 11.0840
   5  0.118453  3.5521  5.2635 -2.5901 16.5322 13.4285 11.8194 11.1424 10.7593
   6  0.133982  3.5625  5.2202 -2.4518 15.8684 12.9418 11.4002 10.7783 10.4228
   7  0.157170  3.5739  5.1736 -2.2903 15.1503 12.3907 10.9286 10.3560 10.0274
   8  0.193935  3.5860  5.1253 -2.1021 14.3683 11.7688 10.3980  9.8694  9.5657
   9  0.233067  3.5953  5.0847 -1.9445 13.7342 11.2608  9.9652  9.4664  9.1794
  10  0.253422  3.5994  5.0635 -1.8706 13.4408 11.0263  9.7653  9.2787  8.9986
  11  0.263033  3.6012  5.0556 -1.8395 13.3177 10.9277  9.6812  9.1997  8.9225
  12  0.269548  3.6022  5.0484 -1.8175 13.2345 10.8597  9.6223  9.1439  8.8684

#AGE= 1.500 EEPS=274
#EEP   M/Mo    LogTeff  LogG   LogL/Lo sdss_u  sdss_g  sdss_r  sdss_i  sdss_z  
   5  0.107932  3.5440  5.2974 -2.6971 17.0780 13.8121 12.1528 11.4242 11.0175
   6  0.121259  3.5542  5.2549 -2.5632 16.3994 13.3328 11.7366 11.0714 10.6940
   7  0.137720  3.5647  5.2112 -2.4220 15.7312 12.8387 11.3119 10.7002 10.3502
   8  0.163277  3.5763  5.1639 -2.2544 14.9976 12.2706 10.8261 10.2628  9.9394
   9  0.199555  3.5876  5.1176 -2.0757 14.2622 11.6834 10.3250  9.8017  9.5010

而且我已经弄清楚如何使用这个(理论上)阅读列:

sdss_g = []
sdss_r = []

with open ("/Users/Wilson/research/isochrones/SDSSugriz/fehm20afep4y33.SDSSugriz") as f:
   for _ in xrange(9):
        next(f)

   for line in f: 
        cols = line.split()
        g = (float(cols[6]))
        r = (float(cols[7]))
        print 'g'
        print g
        print 'r'
        print r
        sdss_g.append(g)
        sdss_r.append(r)

但这不起作用,它给了我这个错误:

Traceback (most recent call last):
  File "isochronereader.py", line 18, in <module>
    g = (float(cols[6]))
IndexError: list index out of range

我很确定这是因为我没有考虑行间距和字符串格式的新标题,但“列表索引超出范围”似乎不是该问题的正确指标。

不过,我不知道如何解释数据之间的标题和空格,我想弄清楚。我还想阅读每个标题中的 AGE。由于这个文本文件的格式,我不能使用 genfromtxt,我不知道该怎么做,特别是因为下一个新标题的行数不统一,所以我不能在具体行数。

最后,我希望能够读取 sdss_g 和 sdss_r 并知道每个 sdss_g 和 sdss_r 元素与什么年龄相关联。有没有简单的方法可以做到这一点?

最佳答案

你可以这样做:

sdss_g = []
sdss_r = []

with open ("/Users/Wilson/research/isochrones/SDSSugriz/fehm20afep4y33.SDSSugriz") as f:
    for line in f:
        if not line.isspace() and not line.startswith("#"):
            print line
            cols = line.split()
            g = (float(cols[6]))
            r = (float(cols[7]))
            print 'g'
            print g
            print 'r'
            print r
            sdss_g.append(g)
            sdss_r.append(r)

关于python - 从文件头获取特定元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31727024/

相关文章:

Python - 如何将单词分成 64 位 block

python - 有效地将索引附加到 Python 中的重复字符串

python - 比较列表中的多个唯一字符串

python - 在 python 2.7 中调用超子类

python - pandas:查找给定列的百分位数统计信息

python - 在参数中使用带有 unicode 字符的 {% url %} 反向 (Django)

python - 列出所有连续的子数组

Python/Django 过滤组中具有最大值的行

python - 使用 pypyodbc 对列名进行 SQL Server 编码

python - Json.dump 失败并出现 'must be unicode, not str' TypeError