简体   繁体   中英

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

Referring to discussion on "Copy content from one YAML to another YAML after comparison of keys" ( Copy content from one YAML to another YAML after comparison of keys )

Is there any way to do this without using ruamel.yaml module. Since I'm using Python 2.7.5, that module is not available.

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

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

Error faced: -

ImportError: No module named ruamel.yaml

Thanks,

Yes, you can simply alter the given code to import yaml instead of ruamel.yaml . You wouldn't have round_trip_load and round_trip_dump , but you can use safe_load and safe_dump instead. This will not keep comments since that is a ruamel feature.

The real problem is not with ruamel.yaml not being available for Python 2.7.5, but with something being wrong with your environment (actual Python configuration, version of pip, your platform). ruamel.yaml works perfectly fine with 2.7.5, as the following shows:

$ 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
$ 

Since you are using an outdated version of Python, you should check if you are not using an outdated version of pip with that. Old versions of pip are known to have caused problems.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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