-2

我正在尝试使用 ply 并使用该库中的 lex.lex 解析单词/短语列表。

我之前在单词列表中使用过 lex.lex,它工作得很好,只是使用 for 循环输入到词法分析器中。

但我不断收到以下错误

Traceback (most recent call last):
File "<pyshell#56>", line 2, in <module>
mylexer.input(a)
File "ply\lex.py", line 253, in input
c = s[:1]
TypeError: 'NoneType' object has no attribute '__getitem__'

我这次尝试使用 lex 的列表是解析为 json 的,据我所知,唯一的区别是,与之前确实有效的 lexing 不同?

谢谢你的帮助。

4

2 回答 2

0

好的

谢谢,在手动阅读列表后,似乎其中一个 json 键没有值,它变成了 None。

于 2013-01-25T14:13:53.647 回答
0

ply.lex的相关代码是:

def input(self,s):
    # Pull off the first character to see if s looks like a string
    c = s[:1]
    if not isinstance(c,StringTypes):
        raise ValueError("Expected a string")
    self.lexdata = s
    self.lexpos = 0
    self.lexlen = len(s)

看来您的问题出在您的电话mylexer.input(a)中。a变量是None而不是单词列表。

有时这是由您生成单词列表的方式引起的。例如,这将导致与您看到的相同的错误:

words = 'the quick brown fox'.split()
a = words.sort()                   # Note, this returns None
mylexer.input(a)                   # The lexer won't be happy with None
于 2013-01-25T04:06:10.500 回答