首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从字符串中提取一些行,同时将括号添加到结果字符串中需要的位置?

如何从字符串中提取一些行,同时将括号添加到结果字符串中需要的位置?
EN

Stack Overflow用户
提问于 2021-07-29 17:16:42
回答 1查看 36关注 0票数 1

我有一个python字符串,它包含Metric-FF规划器的输出,如下所示:

代码语言:javascript
复制
ff: parsing domain file
domain 'BRILLOPROVA' defined
 ... done.
ff: parsing problem file
problem 'TASK' defined
 ... done.


translating negated cond for predicate DIRTY-BLENDER
translating negated cond for predicate EMPTY-BLENDER
no metric specified.

ff: search configuration is Enforced Hill-Climbing, if that fails then best-first search.
Metric is plan length.
NO COST MINIMIZATION (and no cost-minimizing relaxed plans).

Cueing down from goal distance:    6 into depth [1]
                                   5            [1][2][3]
                                   4            [1]
                                   3            [1]
                                   2            [1]
                                   1            [1]
                                   0            

ff: found legal plan as follows
step    0: PICK-UP-GLASS H
        1: PICK-FLAVOR H PAPAYA VASCHETTA
        2: ADD-JUICE MELA H PAPAYA VASCHETTA
        3: BLEND MELA PAPAYA H
        4: TRASH-CUP VASCHETTA H
        5: FILL-GLASS H
        6: WASH-BLENDER
        7: SERVE LEFT

time spent:    0.00 seconds instantiating 10 easy, 0 hard action templates
               0.00 seconds reachability analysis, yielding 14 facts and 10 actions
               0.00 seconds creating final representation with 14 relevant facts, 0 relevant fluents
               0.00 seconds computing LNF
               0.00 seconds building connectivity graph
               0.00 seconds searching, evaluating 12 states, to a max depth of 3
               0.00 seconds total time

我需要对其进行解析,以使结果字符串如下所示:

代码语言:javascript
复制
PICK-UP-GLASS PICK-FLAVOR(PAPAYA) ADD-JUICE BLEND TRASH-CUP FILL-GLASS WASH-BLENDER SERVE(LEFT)

因此,我只需要动作的顺序,只保留动作的一些参数“PICK-SERVE”(确切地说,它将是水果的名称)和"SERVE“(只能有"LEFT”、"RIGHT“和"CENTER”作为参数)。记住,像参数"VASCHETTA“和"H”以及动作的名称("PICK-UP-GLASS","PICK-FLAVOR","ADD-JUICE","BLEND","TRASH-CUP","FILL-GLASS","WASH-BLENDER","SERVE")这样的词对我来说已经很熟悉了,所以我可以用regex很容易地去掉它们,但是像"MELA","PAPAYA“和"LEFT”这样的词可以根据不同的输出而变化。所以我事先不知道它们,似乎也不能操纵字符串来得到我想要的结果。

我已经尝试了下面的代码来尽我所能地修剪字符串:

代码语言:javascript
复制
import re
string = """
ff: parsing domain file
domain 'BRILLOPROVA' defined
 ... done.
ff: parsing problem file
problem 'TASK' defined
 ... done.


translating negated cond for predicate DIRTY-BLENDER
translating negated cond for predicate EMPTY-BLENDER
no metric specified.

ff: search configuration is Enforced Hill-Climbing, if that fails then best-first search.
Metric is plan length.
NO COST MINIMIZATION (and no cost-minimizing relaxed plans).

Cueing down from goal distance:    6 into depth [1]
                                   5            [1][2][3]
                                   4            [1]
                                   3            [1]
                                   2            [1]
                                   1            [1]
                                   0            

ff: found legal plan as follows
step    0: PICK-UP-GLASS H
        1: PICK-FLAVOR H PAPAYA VASCHETTA
        2: ADD-JUICE MELA H PAPAYA VASCHETTA
        3: BLEND MELA PAPAYA H
        4: TRASH-CUP VASCHETTA H
        5: FILL-GLASS H
        6: WASH-BLENDER
        7: SERVE LEFT

time spent:    0.00 seconds instantiating 10 easy, 0 hard action templates
               0.00 seconds reachability analysis, yielding 14 facts and 10 actions
               0.00 seconds creating final representation with 14 relevant facts, 0 relevant fluents
               0.00 seconds computing LNF
               0.00 seconds building connectivity graph
               0.00 seconds searching, evaluating 12 states, to a max depth of 3
               0.00 seconds total time
"""
list = string.split("""ff: found legal plan as follows
step""")
list = list[1].split("time spent:")
steps = list[0]
steps = steps.translate({ord(i): None for i in '0123456789:'})
steps = re.sub("\s+H\s+", " ", steps)
steps = re.sub("\s+VASCHETTA\s+", " ", steps)
steps = re.sub("ADD-JUICE .* BLEND", "ADD-JUICE BLEND", steps)
steps = re.sub("BLEND [A-Z]* [A-Z]* " ,"BLEND " , steps)
list = steps.split("PICK-FLAVOR ")
steps = list[0] + "PICK-FLAVOR(" + list[1]
print(steps)

结果如下:

代码语言:javascript
复制
 PICK-UP-GLASS PICK-FLAVOR(PAPAYA ADD-JUICE BLEND TRASH-CUP FILL-GLASS WASH-BLENDER
     SERVE LEFT

所以我就快完成了,但是我不知道如何添加剩下的圆括号。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-07-29 18:39:17

您可以使用finditer查找您感兴趣的单词。正则表达式模式大致可以这样组织(伪代码):

代码语言:javascript
复制
\d+: (actions|actions-with-param skip-word* param)

实际情况要长一点,因为它命名了每个动作词和要跳过的每个词,并为需要保留的内容添加了捕获组:

代码语言:javascript
复制
res = re.finditer(r"\d+:\s(?:(PICK-UP-GLASS|ADD-JUICE|BLEND|TRASH-CUP|FILL-GLASS|WASH-BLENDER)\b|(PICK-FLAVOR|SERVE) (?:(?:H|VASHETTA)\s)*(\w+))", string)

result = "\n".join(m.group(1) or (m.group(2) + "(" + m.group(3) + ")") for m in res)

print(result)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68573276

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档