gpt4 book ai didi

python - 如何防止 YAML 在没有换行的情况下转储长行

转载 作者:太空狗 更新时间:2023-10-29 17:04:45 25 4
gpt4 key购买 nike

在转储/序列化输入中包含长行的数据时,pyyaml 添加了新行的额外缩进 - 这很烦人,我们如何避免这种两行/多行的转换?

例如

在 [1] 中:

x = "-c /home/user/test/test2/test23/tet/2s/test1/stest/longdirectory1/directory2/ --optnion12 --verbose"

在[2]中:import yaml

在 [3] 中:print (yaml.dump([dict(ATTRIBUTES=[dict(CONFIG=x)])], default_flow_style=False))

错了

- ATTRIBUTES:
- CONFIG: -c /home/user/test/test2/test23/tet/2s/test1/stest/longdirectory1/directory2/
--optnion12 --verbose

应该是这样的

- ATTRIBUTES:
- CONFIG: -c /home/user/test/test2/test23/tet/2s/test1/stest/longdirectory1/directory2/ --optnion12 --verbose

最佳答案

感谢@MathieuMarques 建议查看@dump 选项和提供的链接,YAML 文档不够好,无法找到它。

无论如何,解决方案是为dump 函数指定width 参数。

yaml.dump(数据, width=1000)

@RandomCoder 建议的更好方法使用 yaml.dump(data, width=float("inf")) 作为永久解决方案。

关于python - 如何防止 YAML 在没有换行的情况下转储长行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18514205/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com