Question

I'm currently working on some project using PyFITS. As a beginner with python 3.3, I can't figure out the two errors I get... 1st error------------------------

import pyfits;\
hdulist = pyfits.open('/Users/geo/Desktop/test/casa.fits')\
for i in range(1,26) :\
        str = hdulist[0].header[i];\
        print(str);\
        i=i++;
  File "<ipython-input-41-651183e88e23>", line 3
    for i in range(1,26) :\
      ^
SyntaxError: invalid syntax

Seems weird since when I do the "import" and "hdulist=..." before the "for", like 3 different inputs in console instead of 1, I get no error...

2nd error----------------------- I try to handle the IndexError I get when hdulist[0].header[i]=None. In my case this is true for i=26 or more. So I use except :

try:\
        hdulist[0].header[30]==None\
except:\
        print("end of headers")
  File "<ipython-input-28-fe19468a3999>", line 3
    except:\
         ^
SyntaxError: invalid syntax

I don't know how to solve this, so if you have an idea and are kind enough to help, thank you! ^^ Geo

Was it helpful?

Solution

Well, your syntax is wrong:

  • Indentation matters.
  • The backslashes at the ends of each line mess with your indentation and with ending your statements. They need to go away.
  • Don't end statements with ;, this is Python, not C. Statements end with a newline (which, again, is escaped by your backslash).

Then,

i = i++;

doesn't make much sense in any language, but Python doesn't even have a ++ operator, and Python doesn't need/use semicolons to end a statement.

You want

i += 1

Also, don't use str as a variable name, you're shadowing the built-in type that way.

Furthermore, you never want to use a bare except: - always catch specific exceptions.

Finally, do you really want to compare against None? If so, use

hdulist[0].header[30] is None  # None is a singleton!

But all in all, it looks very much like you should be reading a basic Python tutorial before venturing any further.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top