使用pyparsing很难完成这项特定工作吗? (初学者) [英] Difficulty of this particular job using pyparsing? (beginner)

查看:106
本文介绍了使用pyparsing很难完成这项特定工作吗? (初学者)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个任务要做,我确信Python和pyparsing确实可以帮上忙,但是我对编程仍然还是一个新手,无法就完整实现的挑战性以及是否实现这个问题做出明智的选择.值得尝试,或者肯定会浪费时间.

I have a task to do that I'm sure Python and pyparsing could really help with, but I'm still too much of a novice with programming to make a smart choice about how challenging the complete implementation will be and whether it's worth trying or is certain to be a fruitless time-sink.

任务是按照此语法的通用语法转换任意长度和嵌套深度的字符串:

The task is to translate strings of arbitrary length and nesting depth with a structure following the general grammar of this one:

item12345 'topic(subtopic(sub-subtopic), subtopic2), topic2'

放入这样的字典中的项目:

into an item in a dictionary like this one:

{item12345, 'topic, topic:subtopic, topic:subtopic:sub-subtopic, topic:subtopic2, topic2'}

换句话说,逻辑与数学完全一样,其中括号左端的项分布到括号内的所有内容,,"表示括号内的项,就像加法如何针对因子起作用二项式的.

In other words, the logic is exactly like mathematics where the item immediately to the left of parentheses is distributed to everything inside, and the ',' designates the terms inside of the parentheses, much like how addition functions with respect to factors of a binomial.

到目前为止,我要么为自己发现,要么发现并理解了一些似乎是创建此解决方案所必需的元素的示例.

I've either discovered for myself or found and understood examples of some of the seemingly necessary elements for creating this solution so far.

在Python中解析嵌套表达式:

Parsing nested expressions in Python:

def parenthetic_contents(string):
"""Generate parenthesized contents in string as pairs (level, contents)."""
stack = []
for i, c in enumerate(string):
    if c == '(':
        stack.append(i)
    elif c == ')' and stack:
        start = stack.pop()
        yield (len(stack), string[start + 1: i])

将一个字符串分配给其他字符串:

Distributing one string to others:

from pyparsing import Suppress,Word,ZeroOrMore,alphas,nums,delimitedList

data = '''\
MSE 2110, 3030, 4102
CSE 1000, 2000, 3000
DDE 1400, 4030, 5000
'''

def memorize(t):
    memorize.dept = t[0]

def token(t):
    return "Course: %s %s" % (memorize.dept, int(t[0]))

course = Suppress(Word(alphas).setParseAction(memorize))
number = Word(nums).setParseAction(token)
line = course + delimitedList(number)
lines = ZeroOrMore(line)

final = lines.parseString(data)

for i in final:
    print i

还有其他一些方法,但是这些方法不会直接应用于我的最终解决方案,在我了解python并充分解析以结合想法或找到新想法之前,我还有很长的路要走.

And some others, but these methods won't directly apply to my ultimate solution, and I've still got a ways to go before I understand python and pyparsing well enough to combine the ideas or find new ones.

我一直在通过寻找示例,寻找类似工作的东西,学习更多的python和pyparsing的类和方法来进行研究,但是我不确定我是否足够了解对于我的完整解决方案不起作用的东西,而不仅仅是对一般情况不起作用的中间练习.

I've been hammering away at it by looking for examples, looking for stuff that works similarly, learning more python and more of pyparsing's classes and methods, but I'm not sure how far away I am from knowing enough to make something that works for my full solution rather than just intermediate exercises that won't work for the general case.

所以我的问题是这些.为了完成我想要的工作,我最终将需要多复杂的解决方案?您有什么建议可以帮助我更进一步?

So my questions are these. How complex a solution will I ultimately need in order to do what I'm looking for? What suggestions do you have that might help me get closer?

提前谢谢! (PS-关于StackOverflow的第一篇文章,让我知道我是否需要对此篇文章做任何不同的事情)

Thanks in advance! (PS - first post on StackOverflow, let me know if I need to do anything differently with regard to this post)

推荐答案

在pyparsing中,您的示例如下所示:

In pyparsing, your example would look something like:

from pyparsing import Word,alphanums,Forward,Optional,nestedExpr,delimitedList

topicString = Word(alphanums+'-')
expr = Forward()
expr << topicString + Optional(nestedExpr(content=delimitedList(expr)))

test = 'topic(subtopic(sub-subtopic), subtopic2), topic2'

print delimitedList(expr).parseString(test).asList()

打印

['topic', ['subtopic', ['sub-subtopic'], 'subtopic2'], 'topic2']

转换为topic:subtopic等作为OP的练习.

Converting to topic:subtopic, etc. is left as an exercise for the OP.

这篇关于使用pyparsing很难完成这项特定工作吗? (初学者)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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