2017-10-10 32 views
1

我有一個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} 

我該如何解決這個問題?

回答

1

YAML實例的dump()方法不採用您提供的參數(allow_unicode=True, explicit_start=True)。由於你沒有提供完整的工作程序,我只能猜測你(也)做了import ruamel.yaml as yaml(或甚至import yaml)。

通過的Jinja2插件,其與標準的Jinja2模板語法涉及(通常與YAML解析器解析之前處理)完成的轉換需要在加載和而傾倒工作要做。所以你需要使用相同的YAML(typ='jinja2')實例來完成:

import sys 
file_name = 'meta.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 
yaml.indent(sequence=4, offset=2) 
yaml.preserve_quotes = True 
# yaml.explicit_start = True 
with open(file_name) as fp: 
    data = yaml.load(fp) 

yaml.dump(data, sys.stdout) 

給出確切的往返您的輸入:

{% set name = "pyexcel-ezodf" %} 
{% set version = "0.3.3" %} 
{% set sha256 = "26ddddc61c6bbe2641a15964ba57eaf92a171478e7ed9efb9ae4db1567d0998 
c" %} 

package: 
    name: {{ name|lower }} 
    version: {{ version }} 

source: 
    fn: {{ name }}-{{ version }}.tar.gz 
    # The github url is been used because the tar.gz from pypi is missing the CONT 
RIBUTORS.rst file 
    url: https://github.com/pyexcel/{{ name }}/archive/v{{ version }}.tar.gz 
    sha256: {{ sha256 }} 

build: 
    noarch: python 
    number: 0 
    script: python setup.py install --single-version-externally-managed --record r 
ecord.txt 

requirements: 
    build: 
    - python 
    - setuptools 

    run: 
    - python 
    - lxml 

test: 
    imports: 
    - ezodf 


about: 
    home: https://github.com/pyexcel/pyexcel-ezodf 
    license: MIT 
    license_family: MIT 
    license_file: '{{ environ["RECIPE_DIR"] }}/LICENSE' 
    summary: 'A Python package to create/manipulate OpenDocumentFormat files' 
    description: | 
    'ezodf is a Python package to create new or open existing' + 
    'OpenDocument (ODF) files to extract, add, modify or delete document data' + 
    'forked from dead project https://bitbucket.org/mozman/ezodf' + 
    'format and to/from databases' + 
    '' 
    dev_url: https://github.com/pyexcel/pyexcel-ezodf 

extra: 
    recipe-maintainers: 
    - williamjamir 

你並不需要設置allow_unicode,那就是默認YAML.dump()

+0

@AnthonIt可以是jupyter重寫YAML功能。 – ypriverol

+0

我不知道jupyter在做什麼,但是如果你的代碼仍然是'yaml.dump(data,sys.stdout,allow_unicode = True)',它應該會拋出一個'TypeError'。如果加載和轉儲沒有在同一個上下文中完成,我不知道你的'yaml'是從哪裏來的,但是無論如何,你可以從ruamel.yaml import YAML; yaml = YAML(typ ='jinja2')'就在你轉儲之前 – Anthon

+0

我刪除了我的腳本中的所有複雜性,只使用你的代碼作爲例子。我有這個錯誤:「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