简体   繁体   English

python / pyfits语法错误

[英]python/pyfits syntax errors

I'm currently working on some project using PyFITS. 我目前正在使用PyFITS进行一些项目。 As a beginner with python 3.3, I can't figure out the two errors I get... 1st error------------------------ 作为python 3.3的初学者,我无法弄清楚我得到的两个错误...第一个错误------------------------

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... 似乎很奇怪,因为当我在“ for”之前执行“ import”和“ hdulist = ...”时,就像控制台中的3个不同输入而不是1,我没有收到错误...

2nd error----------------------- I try to handle the IndexError I get when hdulist[0].header[i]=None. 第二个错误---------------------------我尝试处理hdulist [0] .header [i] = None时得到的IndexError。 In my case this is true for i=26 or more. 在我的情况下,对于i = 26或更大的情况,这是正确的。 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 ^^地理

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). ,这是Python,而不是C。语句以换行符结尾(同样,换行符由反斜杠转义)。

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. 任何语言中都没有多大意义,但是Python甚至没有++运算符,并且Python不需要/使用分号来结束语句。

You want 你要

i += 1

Also, don't use str as a variable name, you're shadowing the built-in type that way. 另外,不要将str用作变量名,那样会掩盖内置类型。

Furthermore, you never want to use a bare except: - always catch specific exceptions. 此外,您绝不想使用裸机, except: -总是捕获特定的异常。

Finally, do you really want to compare against None ? 最后,您真的要与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. 但总而言之,看起来非常像您应该在进一步尝试之前阅读基本的Python教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM