How can I add a python tuple to a YAML file using pyYAML?

后端 未结 2 1941
花落未央
花落未央 2021-01-11 23:55

The title is fairly self-explanatory.

When I save a tuple to a YAML file, I get something that looks like this:

ambient:  !!python/tuple [0.3, 0.3 ,0         


        
2条回答
  •  旧巷少年郎
    2021-01-12 00:24

    At least according to the PyYAML documentation:

    The function yaml.safe_load limits this ability to simple Python objects like integers or lists.

    The list, as you can see in the source, is somewhat more extensive but does not include tag:yaml.org,2002:python/tuple.

    It appears that if you are generating a !!python/tuple type in your YAML file, you are using dump() as opposed to safe_dump(). If that's the case, you should probably switch to using load() in place of safe_load(), as files created by dump() are not guaranteed to be loadable by safe_load(). (See the description of safe_dump()).

提交回复
热议问题