[英]Pyparsing for unicode letters
我需要对 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)
有没有办法解决这个问题?
Pyparsing 具有pyparsing_unicode
模块,该模块定义了许多 unicode 字符范围,并在每个范围内定义了alphas
、 nums
等。 范围包括CJK
、 Cyrillic
、 Devanagari
、 Hebrew
、 Arabic
等。 示例目录中的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
。
alphas
显然只是英语/纯 ASCII。 以下似乎有效:
from pyparsing import Word, alphas, alphas8bit
greet = Word(alphas+alphas8bit) + "," + Word(alphas+alphas8bit) + "!"
hello = "Hello, cédille!"
greet.parseString(hello)
这是 Unicode,因此字符é没有什么特别的“8 位”; 但是,如果文档至少大致正确,我想它仍然会与稍微更具异国情调的重音字符(Latin-1 中不可用的任何字符,例如捷克语或波兰语重音字符,或者极端尝试越南语)中断。
也许探索unicodedata
模块以获得“字母”字符的正确枚举,或者找到正确公开此 Unicode 功能的第三方模块。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.