有没有办法在 pyparsing 中使用数字?

发布于 01-09 13:53 字数 200 浏览 5 评论 0原文

我正在 python 3.8 中使用 python 库 PyParsing,我想知道是否有办法解析数字?这是我当前的代码:

from pyparsing import Word, alphas
getWordParse = Word(alphas)

alphas 只是字符,是否也可以使用数字?

I am using the python library PyParsing in python 3.8, and I am wondering if there is a way to parse numbers? Here is my current code:

from pyparsing import Word, alphas
getWordParse = Word(alphas)

alphas is only characters, would there be a way to use numbers as well?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱格式化2025-01-16 13:53:03

Pyparsing 提供了几个用于定义 Word 的方便常量:

alphas = "A-Z" + "a-z"
nums = "0-9"
alphanums = alphas + nums
alphas8bit = "ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþ"
hexnums "0-9" + "A-F" + "a-f"
printables = any non-whitespace character

但这些都只是字符串,您也可以使用字符串文字定义 Word:

word_of_even_digits = Word("02468", as_keyword=True)
word_of_even_digits.search_string("248 125 126 298 42 3.14159").as_list()

打印:

[['248'], ['42']]

Pyparsing 还包括一些常见表达式的解析器(例如 integerrealfnumbernumber),其中还包括解析操作,这些操作会将解析结果返回为 int 或 float,而不仅仅是 str。

这是一个很好的“入门”文档页面: https://pyparsing-docs .readthedocs.io/en/latest/HowToUsePyparsing.html

这是 Word 的参考页面:https://pyparsing-docs.readthedocs.io/ en/latest/pyparsing.html?highlight=Word#pyparsing.Word

请访问 pyparsing wiki 以获取更多信息:https://github.com/pyparsing/pyparsing/wiki

Pyparsing provides several convenience constants for defining Word's:

alphas = "A-Z" + "a-z"
nums = "0-9"
alphanums = alphas + nums
alphas8bit = "ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþ"
hexnums "0-9" + "A-F" + "a-f"
printables = any non-whitespace character

But these are all just strings, and you can define Words using string literals too:

word_of_even_digits = Word("02468", as_keyword=True)
word_of_even_digits.search_string("248 125 126 298 42 3.14159").as_list()

Prints:

[['248'], ['42']]

Pyparsing also includes some parsers for common expressions (like integer, real, fnumber, and number), which also include parse actions that will returned the parsed results as ints or floats, instead of just strs.

Here is a good "getting started" docs page: https://pyparsing-docs.readthedocs.io/en/latest/HowToUsePyparsing.html

And here is the reference page on Word: https://pyparsing-docs.readthedocs.io/en/latest/pyparsing.html?highlight=Word#pyparsing.Word

Visit the pyparsing wiki for more info at: https://github.com/pyparsing/pyparsing/wiki

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文