Using python to dump hexadecimals into YAML

前端 未结 3 756
长发绾君心
长发绾君心 2021-01-15 04:31

Now I\'m dumping into a YAML document. It\'s working as it should for the most part. When I try to dump a hexadecimal such as \"0x2A\" it converts to 42. Isn\'t there any wa

3条回答
  •  轮回少年
    2021-01-15 05:26

    Representing all int in hex format, without a HexInt class, can be done using:

    def hexint_presenter(dumper, data):
        return dumper.represent_int(hex(data))
    yaml.add_representer(int, hexint_presenter)
    

    With reference to this answer.

提交回复
热议问题