0

I have python dict:

data = {
  'foo': 'bar',
  'hello': 'world'
}

How to can I pack this dict to XDR data format?

1 Answers1

0

This really depends on how you want it packed. As key value pairs, you could use the following:

Python 2.x:

    import xdrlib
    
    data = {'foo': 'bar', 'hello': 'world'}
    p = xdrlib.Packer()
    
    for key, value in data.items():
        p.pack_string(key)
        p.pack_string(value)
    
    print p.get_buffer()

Python 3.x:

    import xdrlib
    
    data = {'foo': 'bar', 'hello': 'world'}
    p = xdrlib.Packer()
    
    for key, value in data.items():
        p.pack_string(key.encode())
        p.pack_string(value.encode())
    
    print(p.get_buffer())

Which would display something like:

◦◦◦foo◦◦◦◦bar◦◦◦◦hello◦◦◦◦◦◦world◦◦◦
Jonathan1609
  • 1,809
  • 1
  • 5
  • 22
Martin Evans
  • 45,791
  • 17
  • 81
  • 97