pyparsing

Posted caesarlinsa

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了pyparsing相关的知识,希望对你有一定的参考价值。

1.Word(token)

用于匹配由允许的字符集组成的单词,常见的错误是使用特定字符串Word("expr")匹配"expr"

 - L {alphas}  字母
 - L {nums}  数字
 - L {alphanums} 数字字母混合

 技术图片

2.Suppress

忽略表达式中内容

import pyparsing as pp

source = "a , b, c, d"
wd = pp.Word(pp.alphas)
wd_list = wd + pp.ZeroOrMore(‘,‘+ wd)
print wd_list.parseString(source)
# result [‘a‘, ‘,‘, ‘b‘, ‘,‘, ‘c‘, ‘,‘, ‘d‘]

# ZeroOrMore
wd_list = wd +pp.ZeroOrMore(pp.Suppress(‘,‘)+wd)

print wd_list.parseString(source)
# [‘a‘, ‘b‘, ‘c‘, ‘d‘]

 3. Group 

 使用group将返回的结果,加上[] 结构

from pyparsing import *

wd = Word(alphas)
comma = Literal(",")
greetee = OneOrMore(wd)
end = oneOf("! ?")
greeting = wd + comma + greetee + end
# result::[‘Hello‘, ‘,‘, ‘World‘, ‘!‘]
print greeting.parseString("Hello,World!")
wd = Group(Word(alphas)) comma = Literal(",") greetee = OneOrMore(wd) end = oneOf("! ?") greeting = wd + comma + greetee + end # [[‘Hello‘], ‘,‘, [‘World‘], ‘!‘] print greeting.parseString("Hello,World!")
from pyparsing import *

wd = Word(alphas)
comma = Literal(",").suppress()
greetee = OneOrMore(wd)
end = oneOf("! ?").suppress()
greeting = wd + comma + greetee + end
#[‘Hello‘, ‘World‘]
print greeting.parseString("Hello,World!")

 

 

以上是关于pyparsing的主要内容,如果未能解决你的问题,请参考以下文章

PyParsing 使用大括号和特定标头解析嵌套循环

nest expression & Pyparsing

pyparsing

pyparsing用分号而不是逗号解析csv文件

AWS Lambda python 错误:Runtime.ImportModuleError:无法导入模块“app”:无法从“pyparsing”导入名称“operatorPrecedence”

Python 安装matplotlib,six,dateutil,pyparsing 完整过程