“删除"使用pyparsing的列表 [英] "Deparsing" a list using pyparsing

查看:93
本文介绍了“删除"使用pyparsing的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以对解析的列表进行pypars解析,并使其返回原始字符串?

Is it possible to give pyparsing a parsed list and have it return the original string?

推荐答案

是的,您可以如果已指示解析器不要丢弃任何输入.您可以使用Combine组合器进行操作.

Yes, you can if you've instructed the parser not to throw away any input. You do it with the Combine combinator.

假设您输入的是:

>>> s = 'abc,def,  ghi'

这里是一个解析器,可以获取列表的确切文本:

Here's a parser that grabs the exact text of the list:

>>> from pyparsing import *
>>> myList = Word(alphas) + ZeroOrMore(',' + Optional(White()) + Word(alphas))
>>> myList.leaveWhitespace()
>>> myList.parseString(s)
(['abc', ',', 'def', ',', '  ', 'ghi'], {})

要降级":

>>> reconstitutedList = Combine(myList)
>>> reconstitutedList.parseString(s)
(['abc,def,  ghi'], {})

为您提供初始输入.

但这是有代价的:将所有多余的空白作为标记浮动通常是不方便的,并且您会注意到,我们必须在myList中显式关闭跳过 的空白.这是去除空格的版本:

But this comes at a cost: having all that extra whitespace floating around as tokens is usually not convenient, and you'll note that we had to explicitly turn whitespace skipping off in myList. Here's a version that strips whitespace:

>>> myList = Word(alphas) + ZeroOrMore(',' + Word(alphas))
>>> myList.parseString(s)
(['abc', ',', 'def', ',', 'ghi'], {})
>>> reconstitutedList = Combine(myList, adjacent=False)
>>> reconstitutedList.parseString(s)
(['abc,def,ghi'], {})

请注意,此时您还没有获得文字输入,但这对您来说已经足够了.还要注意,我们必须明确地告诉Combine允许跳过空格.

Note you're not getting the literal input back at this point, but this may be good enough for you. Also note we had to explicitly tell Combine to allow the skipping of whitespace.

尽管如此,实际上,在许多情况下,您甚至都不关心定界符.您希望解析器将重点放在项目本身上.有一个名为commaSeparatedList的函数可以为您方便地剥离定界符和空格:

Really, though, in many cases you don't even care about the delimiters; you want the parser to focus on the items themselves. There's a function called commaSeparatedList that conveniently strips both delimiters and whitespace for you:

>>> myList = commaSeparatedList
>>> myList.parseString(s)
(['abc', 'def', 'ghi'], {})

但是,在这种情况下,"deparsing"步骤没有足够的信息来使重构的字符串有意义:

In this case, though, the "deparsing" step doesn't have enough information for the reconstituted string to make sense:

>>> reconstitutedList = Combine(myList, adjacent=False)
>>> reconstitutedList.parseString(s)
(['abcdefghi'], {})

这篇关于“删除"使用pyparsing的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆