在读取HDU的“while”循环中找不到我的错误

时间:2013-07-08 09:43:50

标签: python fits pyfits

一旦成为初学者,永远是初学者! 我正在使用python 2.7.5,OSX 10.8

即使你不了解pyfits,你也可以解决我的问题,因为我认为这是我的算法中的一个问题! 我使用以下代码

#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import pyfits
from PySide import QtGui, QtCore
import os
import fonctions
print('\n\n')

directory = raw_input("file path : ")

hdulist=pyfits.open(directory)
print('\n\n')
print('--------------------------------fits informations :')
hdulist.info()
print ('\n\n')


print("\n")
j=0 #PyFITS is using zero-based indexing when referring to HDUs
k=0  
while True: #runs through all the HDUs
    try:    
        hdulist[j].header[k] is None
    except IndexError:  #errors handling when coming to the last HDU
        print("--------------------------------No more HDU! \n\n\n\n\n\n")
        break

    while True: #runs through all the headers
        try:
            hdulist[j].header[k] is None
        except IndexError:  #errors handling when coming to the last header
            i=0
            break
        header = hdulist[j].header[k]
        print (hdulist[j].header.ascardlist())
        k=k+1
    j=j+1

它“工作”,因为它显示hdulist [j] .header.ascardlist(),但它打印了k次,然后去下一个HDU ......有什么建议吗?

3 个答案:

答案 0 :(得分:1)

我从未与Pyfits合作,但我查看了文档。 我会说这个循环结构,应该更合适

for hdu in hdulist:
    for hdu_header in hdu.header.itervalues():
        print( hdu_header.ascardlist() )

希望我能帮到你。

答案 1 :(得分:0)

我确信赋值k = 0需要进入外部'while True'循环 因为需要为每个新j重置它。

答案 2 :(得分:0)

你没有真正解释这应该做什么,但看起来你只是试图在HDU中显示所有标题。没有必要的while语句或try / excepts。这应该足够了:

with pyfits.open(filename) as hdulist:
    for hdu in hdulist:
        print hdu.header