-1

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

Iguananaut
  • 21,810
  • 5
  • 50
  • 63
Geo
  • 119
  • 1
  • 10
  • 2
    Why do you put `\\` at the end of each line? – awesoon Jun 21 '13 at 08:48
  • Remove the backslashes and semi-colons. Indentation matters in Python, so take care how you indent. And `i++` does not exist in Python (also, even in C, `i=i++` is a hideous statement). –  Jun 21 '13 at 08:51
  • Also for what it's worth a value in a FITS header will never be `None`. It will only ever be values of the type that can be stored in FITS headers (strings, numbers, or bools, basically). – Iguananaut Sep 10 '15 at 21:56

1 Answers1

4

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.

Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
  • Thank you! Yeah I changed the i++ already. I saw the "backslashes thing" on a site, when looking for a "how to write more than one line in the console" help... How do you do more than one line then, like when you want to write a "for"? – Geo Jun 21 '13 at 09:01
  • You only need backslashes if you want to split a *single* statement that *should* be only on one line over multiple lines. You hardly ever need to do that in Python. Read the tutorial, look at the examples, try them in the console. For example, type `for i in range(10):`, press Enter, you'll see the prompt `...` that shows you Python is now waiting for the next command that belongs in this block. Type four spaces for indentation, enter `print(i)`, press Enter twice (to tell Python to end that block). Done. – Tim Pietzcker Jun 21 '13 at 09:05