简体   繁体   English

没有新行的Yaml转储

[英]Yaml dump without new line

I am not sure if rules for Yaml dictates new line as a must have, But I am having an issue with the new line characters in the Yaml serialized string. 我不确定Yaml的规则是否必须规定换行,但是Yaml序列化字符串中的换行字符存在问题。 Here is what I am trying to do: 这是我正在尝试做的事情:

yaml.dump(my_python_dict)

Returns: 返回值:


"---\nasset_data:\n- {assetDefaultScenePath: 'C:\\\\data\\\\my_newfile.ma',\n  assetName: new_asset}\n- {assetDefaultScenePath: 'C:\\\\assetA.ma',\n  assetName: assetA.ma}\naudio_dir: ''\nbg_dir: ''\ndestination: ''\nepisode: '00'\nprod: test\n"

The issue is the YAML string is generated from Ruby and is passed through an intermediate scripting language (MEL) before it reaches Python. 问题是YAML字符串是从Ruby生成的,并在到达Python之前通过中间脚本语言(MEL)传递。 The intermediate language cannot understand Yaml and computes all the "\\n" and fails. 中间语言无法理解Yaml并计算所有的“ \\ n”并失败。 I was wondering if there is a way to dump Yaml without a newline character? 我想知道是否有一种方法可以在没有换行符的情况下转储Yaml? I am guessing that would cause issues at load. 我猜这会在加载时引起问题。 I am currently out of idea's and any recommendation would be helpful. 我目前不在主意范围内,任何建议都将有所帮助。

Here is the detail on how things are tied up. 这是有关如何捆绑事物的详细信息。 The ruby code will build a Windows Command line and execute it. 红宝石代码将构建一个Windows命令行并执行它。

"Mayabatch" -command "source \"C:/cygwin/home/workspace/bpt-maya/src/bd_lay/generate.mel\";generate(\”{SERIALIZED_DICT}\")"
  1. SERIALIZED DICT is where the YAML data needs to be added. SERIALIZED DICT是需要添加YAML数据的位置。
  2. generate.mel has a python call with SERIALIZED DICT as argument generate.mel有一个以SERIALIZED DICT作为参数的python调用
  3. The Mayabatch does not support direct Python execution thats why it needs to be wrapped. Mayabatch不支持直接Python执行,这就是为什么需要包装的原因。 JSON will fail here due to the double quotes that's why I need some other serialization. 由于双引号,JSON将在此处失败,这就是为什么我需要其他一些序列化的原因。

YAML is a super-set of JSON. YAML是JSON的超集。 So, a valid JSON is also a valid YAML (but valid YAML isn't necessarily valid JSON). 因此,有效的JSON也是有效的YAML(但有效的YAML不一定是有效的JSON)。

This fact may allow one to get around MEL's YAML-parsing limitations 这一事实可能使人绕过MEL的YAML分析限制

>>> import yaml
>>> import json

>>> orig_yaml_txt = "---\nasset_data:\n- {assetDefaultScenePath: 'C:\\\\data\\\\my_newfile.ma',\n  assetName: new_asset}\n- {assetDefaultScenePath: 'C:\\\\assetA.ma',\n  assetName: assetA.ma}\naudio_dir: ''\nbg_dir: ''\ndestination: ''\nepisode: '00'\nprod: test\n"
>>> dict_1 = yaml.load(orig_yaml_txt)
>>> intermediate_json_txt = json.dumps(dict_1)
>>> dict_2 = yaml.load(intermediate_json_txt)
>>> dict_1 == dict_2
True

In the above snippet, I take your YAML-serialized text, load it into a python dictionary, then dump that to a json-serialized text, and then load it into a python dictionary using YAML to parse. 在上面的代码段中,我将您的YAML序列化的文本加载到python字典中,然后将其转储到json序列化的文本中,然后使用YAML进行解析将其加载到python字典中。

In your question, you state that the original YAML is generated from Ruby. 在您的问题中,您声明原始的YAML是从Ruby生成的。 Perhaps, instead of dumping YAML, you could dump JSON from Ruby, or add another step in the pipeline that converts the YAML to JSON before being fed to MEL. 也许,除了转储YAML之外,您还可以从Ruby转储JSON,或者在流水线中添加将YAML转换为JSON的另一步骤,然后再将其输入MEL。

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

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