Reading c struct via python

Lakshmipathi.G lakshmipathi.g at gmail.com
Mon Nov 11 23:14:51 EST 2013


Hi -
We have C code which writes following struct into berkeley db ("my_db.db").

struct my_info {
    unsigned long int i, e;
    int o;
    char *f;
    char *s;
};

How to read this via Python? Google search gave this code
---
$ cat pybsd2.py
from bsddb import db
fruitDB = db.DB()
fruitDB.open('my_db.db', None, db.DB_BTREE, db.DB_DIRTY_READ)
cursor = fruitDB.cursor()
rec = cursor.first()
while rec:
    print rec
    rec = cursor.next()
fruitDB.close()
---

While storing an entry (o=>500,f=>/home/laks/abcde,s=>OSr,i=>4668368
,e=>10000) and reading it back i get

$ python pybsd2.py
("\x10'\x00\x00\x00\x00\x00\x00",
"\xf4\x01\x00\x00\xd0;G\x00\x00\x00\x00\x00\x10'\x00\x00\x00\x00\x00\x00/home/laks/abcde\x00OSr\x00")

And this http://docs.python.org/2/library/bsddb.html says bdb module is
removed  recently.

Further Searching provides modules like cpickle,ctypes,struct - not sure
which is the right approach/way to proceed.
Should we proceed with bdb module and find out how to retrive integer from
db or use others like ctypes/(c)pickle/struct etc?

Thanks for any help.

Ps : If this question already answered and discussed often, please redirect
me to that thread.

-- 
----
Cheers,
Lakshmipathi.G
FOSS Programmer.
www.giis.co.in
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20131112/74b16315/attachment.html>


More information about the Python-list mailing list