簡體   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