Loading and dumping hex values with OrderedDict

191 views Asked by At

I'm using the OrderedLoader from this answer. It's working great keeping the order of the file but my YAMLs have some hex fields, like:

  fieldA:
   subA: foo
   subB: 0xff00
   subC: 0x00aa

that are being converted into int fields at load time:

 ('fieldA', OrderedDict([('subA', 'foo'), ('subB', 65280), ('subC', 170)

and then dumped like:

fieldA:
  subA: foo
  subB: 65280
  subC: 170

does anybody how I can prevent this from happening? I've been dealing for a while with safe_dump BaseLoader and so on with no luck.

Thanks!

1

There are 1 answers

0
Ignacio Verona On BEST ANSWER

I just ended up by enclosing the hex numbers with quotes in the original YAML file:

  fieldA:
   subA: foo
   subB: '0xff00'
   subC: '0x00aa'

now PyYAML is not trying to convert them.

Thanks!