我需要对unicode字符使用pyparsing。所以我尝试了从他们的github存储库中使用法语字符cédille的简单示例,并给出了错误。
我的代码
from pyparsing import Word, alphas
greet = Word(alphas) + "," + Word(alphas) + "!"
hello = "Hello, cédille!"
greet.parseString(hello)它会给出错误
pyparsing.ParseException: Expected "!" (at char 8), (line:1, col:9)有没有办法解决这个问题?
发布于 2019-11-30 00:24:37
Pyparsing具有pyparsing_unicode模块,该模块定义了许多unicode字符范围,并在每个范围内定义了alphas、nums等。范围包括CJK、Cyrillic、Devanagari、Hebrew、Arabic等。examples目录中的greetingInGreek.py和greetingInKorean.py示例展示了其中的几个实际操作。
使用Latin1集的示例将如下所示:
from pyparsing import Word, pyparsing_unicode as ppu
intl_alphas = ppu.Latin1.alphas
greet = Word(intl_alphas) + "," + Word(intl_alphas) + "!"
hello = "Hello, cédille!"
print(greet.parseString(hello))打印:
['Hello', ',', 'cédille', '!']alphas8bit可能会被保留用于遗留支持,但新的应用程序应该使用pyparsing_unicode.Latin1.alphas。
发布于 2019-11-29 22:53:33
alphas显然只是英语/纯ASCII码。以下内容似乎可以正常工作:
from pyparsing import Word, alphas, alphas8bit
greet = Word(alphas+alphas8bit) + "," + Word(alphas+alphas8bit) + "!"
hello = "Hello, cédille!"
greet.parseString(hello)这是Unicode,所以字符é没有什么特别的“8位”;但是如果文档至少是大致正确的,我猜它仍然会使用稍微有点异国情调的重音字符(任何在拉丁语-1中不可用的字符,比如捷克语或波兰语重音字符,或者尝试越南语)。
也许可以探索unicodedata模块来获得“字母”字符的正确枚举,或者找到一个第三方模块来正确地公开这个Unicode特性。
https://stackoverflow.com/questions/59106516
复制相似问题