Python:isalpha()函数中的NoneType错误

时间:2016-12-05 23:46:39

标签: python-3.x attributeerror nonetype isalpha

我目前遇到的问题是NoneType并在Python中使用函数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'

如果错误在程序开始时上升,我不会感到惊讶。但它有可能在线路末端上升?是否有可能错误以某种方式与行尾字符#34; \ n&#34;相关联。感谢您的建议:)

2 个答案:

答案 0 :(得分:0)

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

tmp = check_chars(tmp,x)

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

tmp.isalpha()

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

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

答案 1 :(得分:0)

我同意Paul Griffiths的观点。我建议您在使用check_chars()或在其中之前检查分隔符和运算符,因为这会因返回的tmp而导致错误。注意:数字无法处理。

这是处理错误时发生的完整输出,以便您引用要处理的案例。注意:我使用return&#34;&#34;当使用&#34;除了&#34;在check_chars()中,只是为了避免下次再出现错误。我也使用&#34; x = x.strip()&#34;在输入x之前避免换行/空白字符(除非你想要它们)。 另请注意我使用&#34;&lt;&lt;&#34;和&#34;&gt;&gt;&#34;包含变量的值。

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

(对读者:是否有某种方法可以减少此输出,例如扰流板?)