python-3.xxdr

how to write xdr packed data in binary files


I want to convert an array into xdr format and save it in binary format. Here's my code:

# myData is Pandas data frame, whose 3rd col is int (but it could anything else)

import xdrlib

p=xdrlib.Packer()
p.pack_list(myData[2],p.pack_int)

newFile=open("C:\\Temp\\test.bin","wb")
# not sure what to put
# p.get_buffer() returns a string as per document, but how can I provide xdr object?
newFile.write(???) 
newFile.close()

How can I provide the XDR-"packed" data to newFile.write function?

Thanks


Solution

  • XDR is a pretty raw data format. Its specification (RFC 1832) doesn't specify any file headers, or anything else, beyond the encoding of various data types.

    The binary string you get from p.get_buffer() is the XDR encoding of the data you've fed to p. There is no other kind of XDR object.

    I suspect that what you want is simply newFile.write(p.get_buffer()).

    Unrelated to the XDR question, I'd suggest using a with statement to take care of closing the file.