1

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!

Community
  • 1
  • 1
Ignacio Verona
  • 655
  • 2
  • 8
  • 22
  • What happens when you do `od['subB'] = hex(od['subB'])`? Does the output become a single-quoted string? (`od` is a reference to your `OrderedDict`.) – Steven Rumbalski Nov 28 '14 at 18:34
  • @StevenRumbalski, if I do that then the right value is displayed. MY initial fix for this is to run a loop over all the items and covert them back to the "right" format, but I'd rather to look for the elegant solution. – Ignacio Verona Nov 28 '14 at 19:00

1 Answers1

0

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!

Ignacio Verona
  • 655
  • 2
  • 8
  • 22