繁体   English   中英

我们可以在不使用 ruamel.yaml 的情况下将旧 YAML 的内容复制到新 YAML 吗

[英]Can we copy content of old YAML to new YAML without using ruamel.yaml

参考“Copy content from one YAML after compare key to another YAML”( Copy content from one YAML to another YAML after compare key

有没有办法在不使用 ruamel.yaml 模块的情况下做到这一点。 由于我使用的是 Python 2.7.5,因此该模块不可用。

#!/usr/local/bin/python2.7.5

import os.path
import yaml
import sys
import ruamel.yaml

面临的错误:-

ImportError: No module named ruamel.yaml

谢谢,

是的,您可以简单地更改给定的代码以import yaml而不是ruamel.yaml 你不会有round_trip_loadround_trip_dump ,但你可以使用safe_loadsafe_dump代替。 这不会保留评论,因为这是一个 ruamel 功能。

真正的问题不是 ruamel.yaml 不适用于 Python 2.7.5,而是您的环境有问题(实际的 Python 配置、pip 版本、您的平台)。 ruamel.yaml与 2.7.5 完美配合,如下所示:

$ virtualenv -p /opt/python/2.7.5/bin/python /tmp/so_47987860
Running virtualenv with interpreter /opt/python/2.7.5/bin/python
New python executable in /tmp/so_47987860/bin/python
Installing setuptools, pip, wheel...done.
$ source /tmp/so_47987860/bin/activate
(so_47987860) $ python --version
Python 2.7.5
(so_47987860) $ pip --version
pip 9.0.1 from /tmp/so_47987860/lib/python2.7/site-packages (python 2.7)
(so_47987860) $ pip install ruamel.yaml
Collecting ruamel.yaml
/tmp/so_47987860/lib/python2.7/site-packages/pip/_vendor/requests/packages/urllib3/util/ssl_.py:318: SNIMissingWarning: An HTTPS request has been made, but the SNI (Subject Name Indication) extension to TLS is not available on this platform. This may cause the server to present an incorrect TLS certificate, which can cause validation failures. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/security.html#snimissingwarning.
  SNIMissingWarning
/tmp/so_47987860/lib/python2.7/site-packages/pip/_vendor/requests/packages/urllib3/util/ssl_.py:122: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/security.html#insecureplatformwarning.
  InsecurePlatformWarning
  Downloading ruamel.yaml-0.15.35-cp27-cp27mu-manylinux1_x86_64.whl (534kB)
    100% |████████████████████████████████| 542kB 1.5MB/s 
Collecting ruamel.ordereddict; platform_python_implementation == "CPython" and python_version <= "2.7" (from ruamel.yaml)
  Using cached ruamel.ordereddict-0.4.13-cp27-cp27mu-manylinux1_x86_64.whl
Installing collected packages: ruamel.ordereddict, ruamel.yaml
Successfully installed ruamel.ordereddict-0.4.13 ruamel.yaml-0.15.35
(so_47987860) $ python
Python 2.7.5 (default, Dec 29 2017, 09:46:55) 
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import ruamel.yaml
>>> yaml = ruamel.yaml.YAML()
>>> import sys
>>> yaml.dump(dict(working_normal='2.7.5'), sys.stdout)
working_normal: 2.7.5
>>> exit()
(so_47987860) $ deactivate
$ 

由于您使用的是过时版本的 Python,您应该检查您是否使用过时版本的pip 已知旧版本的pip会导致问题。

暂无
暂无

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

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