How to prevent YAML to dump long line without new line(如何防止 YAML 在没有新行的情况下转储长行)
问题描述
在转储/序列化输入中具有长行的数据时,pyyaml 会在新行中添加额外的缩进 - 这很烦人,我们如何避免在两行/多行中进行这种转换?
While dumping/serializing data having long lines in input, pyyaml adds extra indentation with new line - which is annoying, how can we avoid this conversion in two lines / multiple lines ?
例如
在 [1] 中:
x = "-c /home/user/test/test2/test23/tet/2s/test1/stest/longdirectory1/directory2/ --optnion12 --verbose"
在[2]中:导入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 文档不够好,无法找到.
Thanks @MathieuMarques for suggesting to look @ dump options and link provided, YAML documentation was not good enough to find it out.
无论如何,解决方案是为 dump 函数指定 width 参数.
Anyways solution is to specify width parameter for dump function.
即yaml.dump(data, width=1000)
@RandomCoder 建议使用 yaml.dump(data, width=float("inf")) 永久解决方案.
A better approach suggested by @RandomCoder to use yaml.dump(data, width=float("inf")) for a permanent solution.
这篇关于如何防止 YAML 在没有新行的情况下转储长行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何防止 YAML 在没有新行的情况下转储长行
基础教程推荐
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
