如果在文本文件中的两个不同字符之间,Python

If between two different characters in a text file, Python

我基本上是在尝试使用 python 进行查找和替换,但使其仅适用于“{s:”和后面的“}”之间的字符串。我有一个包含以下许多内容的长文本文件:

["c", "DashedSentence", {s: "Yo limpio mi cuarto todos los sábados."},
      "Question",       {q: "¿Cuándo limpio mi cuarto?",
                         as: ["Todos los sábados.",
                              "Todos los domingos."]}],

["c", "DashedSentence", {s: "Nosotros contestamos el correo cada semana."},
      "Question",       {q: "¿Con qué frecuencia contestamos el correo?",
                         as: ["Cada semana.",
                              "Cada dos semanas."]}],

最后,我希望在 "s:" 部分中用下划线将短语组合在一起,将“ mi ”替换为“ mi_” 以产生 "mi_cuarto",与 "los" 类似"el" ...以及更多未在给定示例中的内容。

目前我只有:

s = open("stimuli.txt").read()

word = [' mi ','los ']
phrase = [' mi_',' los_']

for i in range(len(word)):
    if BETWEEN "{s:" and "},":
        s = s.replace(word[i],phrase[i])

f = open("stimuli_phrases.txt", 'w')
f.write(file)

当然,BETWEEN 不是真实的,这正是我要找的。我可能没有以正确的方式解决问题,所以我也愿意接受任何其他想法!感谢您的帮助,谢谢!

编辑:所需的输出将 {s:} 部分中的名词短语和介词短语分组,如下所示:

["c", "DashedSentence", {s: "Yo limpio mi_cuarto todos_los_sábados."},
      "Question",       {q: "¿Cuándo limpio mi cuarto?",
                         as: ["Todos los sábados.",
                              "Todos los domingos."]}],

["c", "DashedSentence", {s: "Nosotros contestamos el_correo cada_semana."},
      "Question",       {q: "¿Con qué frecuencia contestamos el correo?",
                         as: ["Cada semana.",
                              "Cada dos semanas."]}],

你给的文件是JSON formatted, which mean it could easily be parsed with the builtin python json library:

import json

with open("/path/to/your/file", "r") as f:
    data = json.load(f)

for item in data:
    try:
        s = item['s']
    except (TypeError, KeyError):
        pass

当然,如果您不想或可以将此文件解析为 json,您可以使用 re library:

import re
to_process = re.findall("{s:\"(.+)}\"", yourtext)

To learn or practice with regex, look at there: https://regexr.com/