我有一个Python脚本,想读一组畅达食谱,并与一些信息进行更新(如文件:https://github.com/williamjamir/staged-recipes/blob/850760fb63c0fc000b95ac27680ec018fa94dcb7/recipes/pyexcel-ezodf/meta.yaml):YAML文件畅达包更新程序
我使用这个:
from ruamel.yaml import YAML
from yaml.constructor import ConstructorError
from yaml.scanner import ScannerError
yaml = YAML(typ='jinja2')
yaml.allow_duplicate_keys = True
with open(file_name) as fp:
yalm_file = yaml.load(fp)
当我打印使用原始文件yaml_file
:
with open(path_file, 'w') as fp:
yaml.dump(yaml_file, fp, allow_unicode=True, explicit_start=True)
输出包含与类型很多标签和注释 的数据如下:
--- !!python/object/apply:ruamel.yaml.comments.CommentedMap
dictitems:
about: !!python/object/apply:ruamel.yaml.comments.CommentedMap
dictitems: {home: 'https://github.com/soedinglab/xxmotif', license:
GPLv3, license_file: LICENSE,
summary: 'eXhaustive, weight matriX-based motif discovery in nucleotide sequences'}
state:
_yaml_format: !!python/object/new:ruamel.yaml.comments.Format
state: !!python/tuple
- null
- {_flow_style: false}
我该如何解决这个问题?
@AnthonIt可以是jupyter重写YAML功能。 – ypriverol
我不知道jupyter在做什么,但是如果你的代码仍然是'yaml.dump(data,sys.stdout,allow_unicode = True)',它应该会抛出一个'TypeError'。如果加载和转储没有在同一个上下文中完成,我不知道你的'yaml'是从哪里来的,但是无论如何,你可以从ruamel.yaml import YAML; yaml = YAML(typ ='jinja2')'就在你转储之前 – Anthon
我删除了我的脚本中的所有复杂性,只使用你的代码作为例子。我有这个错误:“File”/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/ruamel/yaml/jinja2/__plug_in__.py“,第58行,写在 data = data.replace(k,self.reverse [k]) TypeError:期望的字节,bytearray或缓冲区兼容对象 – ypriverol