简体   繁体   English

如何使用 python 替换此 yaml 文件中的示例内容?

[英]how to replace the examples content in this yaml file using python?

- intent: magic

  examples: |
    - magic
    - Magick
    - jadu
    - magickkk

I want to replace "magic" with other any words.我想用其他任何词替换“魔术”。 How can i do it?我该怎么做? Please help.请帮忙。

There are some yml libraries that can help you like ruamel.yaml or pyyaml .有一些 yml 库可以帮助您喜欢ruamel.yamlpyyaml I always used pyyaml but i found it a little bit triky to produce format you want.我一直使用 pyyaml,但我发现生成你想要的格式有点棘手。 I managed to produce your format in output more easier with ruamel.yaml.我设法使用 ruamel.yaml 更轻松地在 output 中生成您的格式。 However i think these library works in a similar way, you can use what you want.但是我认为这些库以类似的方式工作,你可以使用你想要的。 I tried this我试过这个

from ruamel.yaml import YAML

yaml = YAML()
with open('file.yml') as file:
    data = yaml.load(file)
    print(data)
    data[0]['intent'] = "greetings"
    with open('file.yml', 'w') as out:
        yaml.dump(data, out)

The output will be output 将是

- intent: greetings
    examples: |
      - magic
      - Magick
      - jadu
      - magickkk

I don't understand why, but sometimes the output format is我不明白为什么,但有时 output 格式是

- intent: greetings
    examples: |-
      - magic
      - Magick
      - jadu
      - magickkk

But i don't think that the "-" is a problem.但我不认为“-”是个问题。 In fact yml file is readed correctly.事实上 yml 文件被正确读取。

Am i wrong or this is rasa nlu.yml file?我错了还是这是 rasa nlu.yml 文件? :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM