Python:Isalpha()函数中的非类型错误



我当前处于python中的nontype和使用函数isalpha()的问题。我想做的是词汇分析,我想将整个代码分为四类。这是我的代码

import sys
import keyword  #keyword.iskeyword()
**strong text**def check_chars(tmp,x):
if (tmp == "" or tmp.isalpha()) and (x =="" or x.isalpha()):
    if keyword.iskeyword(tmp):
        print("Key: "+ tmp)
        return ""
    return tmp+x    
if tmp.isalpha() and not x.isalpha():
    print("Var: "+tmp)
    return x        

separators = ['(',')','[',']','{','}',',',':','.',';','@','=','->','+=','-=','*=','/=','//=','%=','@=','&=','|=','^=','>>=','<<=','**=']
operators = ['+','-','*','**','/','//','%','<<','>>','&','|','^','~','<','>','<=','>=','==','!=']
f = open(sys.argv[1],'r')
program = f.read()
tmp = ""
for x in program:
tmp = check_chars(tmp,x)
if tmp in separators:
    print("Sep: "+ tmp)
    tmp = ""
if tmp in operators:    
    print("Ope: "+ tmp)
    tmp = ""
if x is " ":
    tmp = ""

当我到达示例程序中的第一行的末端时,就是这样:

def funkce(a,b):
c=''
a**=b
if a<b:
    print('ahoj'ky',a)
else:
    print(0xff,0b11101,0o777,.90e-10,123E+5,c)
    print('''To je dlouhy
    retezec pres mnoho
    radku''')
funkce(-256+356,.85**.33)

发生错误:

Traceback (most recent call last):
File "HW09.py", line 24, in <module>
tmp = check_chars(tmp,x)
File "HW09.py", line 5, in check_chars
if (tmp == "" or tmp.isalpha()) and (x =="" or x.isalpha()):
AttributeError: 'NoneType' object has no attribute 'isalpha'

,如果该程序开始出现错误,我不会感到惊讶。但是,如何在线结束时可能会上升呢?是否有可能以某种方式与行字符" n"的结尾连接。谢谢您的建议:)

您的问题是,并非所有通过check_chars()函数的路径返回值。因此,有时(特别是在您的情况下,当tmp包含'n'时):

tmp = check_chars(tmp,x)

None分配给tmp,然后下次致电check_chars()时,此:

tmp.isalpha()

尝试在None上致电isalpha(),并引起您的问题。

解决方案是确保通过check_chars()的所有路径返回值。

我同意保罗·格里菲斯(Paul Griffiths)。我建议您在使用check_chars()或内部之前对分隔符和操作员进行检查,因为这会导致由于返回的TMP而导致错误。注意:数字未处理。

这是当处理错误时发生的完整输出,因此您可以在案件处理中引用。注意:我使用return"当使用" ext"时,在check_chars()中使用"除",只是为了避免下次发生错误。在输入X之前,我还使用" X = X.Strip()"以避免使用newline/whitespace chars(除非您想要它们)。另请注意,我使用"&lt;&lt;"one_answers">>"包含变量的值。

Key: def
Var: funkce
Sep: (
Var: a
Sep: ,
Var: b
Sep: )
An error occurred. x=<<>>, tmp=<<None>>
Var: c
Sep: =
An error occurred. x=<<'>>, tmp=<<None>>
Var: a
Ope: *
An error occurred. x=<<=>>, tmp=<<None>>
Var: bifa
Ope: <
Var: b
Sep: :
Var: print
Sep: (
An error occurred. x=<<a>>, tmp=<<None>>
Var: hoj
An error occurred. x=<<k>>, tmp=<<None>>
Var: y
An error occurred. x=<<a>>, tmp=<<None>>
An error occurred. x=<<>>, tmp=<<None>>
Var: else
Sep: :
Var: print
Sep: (
An error occurred. x=<<x>>, tmp=<<None>>
Var: ff
Sep: ,
An error occurred. x=<<b>>, tmp=<<None>>
An error occurred. x=<<1>>, tmp=<<None>>
An error occurred. x=<<0>>, tmp=<<None>>
An error occurred. x=<<,>>, tmp=<<None>>
An error occurred. x=<<o>>, tmp=<<None>>
An error occurred. x=<<7>>, tmp=<<None>>
An error occurred. x=<<,>>, tmp=<<None>>
An error occurred. x=<<9>>, tmp=<<None>>
An error occurred. x=<<e>>, tmp=<<None>>
An error occurred. x=<<1>>, tmp=<<None>>
An error occurred. x=<<,>>, tmp=<<None>>
An error occurred. x=<<2>>, tmp=<<None>>
An error occurred. x=<<E>>, tmp=<<None>>
An error occurred. x=<<5>>, tmp=<<None>>
An error occurred. x=<<c>>, tmp=<<None>>
An error occurred. x=<<>>, tmp=<<None>>
Var: print
Sep: (
An error occurred. x=<<'>>, tmp=<<None>>
An error occurred. x=<<T>>, tmp=<<None>>
Var: ojedlouhyretezecpresmnohoradku
An error occurred. x=<<'>>, tmp=<<None>>
An error occurred. x=<<>>, tmp=<<None>>
Var: funkce
Sep: (
An error occurred. x=<<2>>, tmp=<<None>>
An error occurred. x=<<6>>, tmp=<<None>>
An error occurred. x=<<3>>, tmp=<<None>>
An error occurred. x=<<6>>, tmp=<<None>>
An error occurred. x=<<.>>, tmp=<<None>>
An error occurred. x=<<5>>, tmp=<<None>>
An error occurred. x=<<*>>, tmp=<<None>>
An error occurred. x=<<3>>, tmp=<<None>>
An error occurred. x=<<)>>, tmp=<<None>>
Process finished with exit code 0

(致读者:有没有办法最小化此输出,例如扰流板?)

相关内容

  • 没有找到相关文章

最新更新