Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #59274

Re: Reading c struct via python

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'output': 0.05; 'string.': 0.05; '64-bit': 0.07; 'binary': 0.07; 'modify': 0.07; 'string': 0.09; '32-bit': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'strings.': 0.09; 'terminated': 0.09; 'python': 0.11; '"e"': 0.16; '"n"': 0.16; '"s"': 0.16; '*s;': 0.16; 'data...': 0.16; 'message-id:@4ax.com': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'skip:" 100': 0.16; 'skipping': 0.16; 'storing': 0.16; 'subject:Reading': 0.16; 'unsigned': 0.16; 'subject:python': 0.16; 'variable': 0.18; 'module': 0.19; '>>>': 0.22; 'appears': 0.22; 'import': 0.22; 'python?': 0.22; 'byte': 0.24; 'char': 0.24; 'module,': 0.24; 'url:home': 0.24; 'looks': 0.24; 'skip:" 30': 0.26; 'header:X-Complaints-To:1': 0.27; 'fixed': 0.29; 'rest': 0.29; 'character': 0.29; "doesn't": 0.30; 'skip:( 40': 0.30; 'skip:( 20': 0.30; 'code': 0.31; "skip:' 10": 0.31; 'struct': 0.31; 'but': 0.35; 'google': 0.35; 'really': 0.36; 'skip:" 50': 0.36; 'entry': 0.36; 'charset:us-ascii': 0.36; 'unit': 0.37; 'two': 0.37; 'received:76': 0.38; 'nov': 0.38; 'writes': 0.38; 'handle': 0.38; 'to:addr:python-list': 0.38; 'previous': 0.38; 'structure': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'read': 0.60; 'length': 0.61; 'first': 0.61; 'back': 0.62; "you'll": 0.62; 'here': 0.66; 'c-type': 0.84; 'subject:via': 0.84; 'directly.': 0.95; '2013': 0.98
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Dennis Lee Bieber <wlfraed@ix.netcom.com>
Subject Re: Reading c struct via python
Date Tue, 12 Nov 2013 20:40:07 -0500
Organization IISS Elusive Unicorn
References <CAKuJGC_-i66xtk0aitHKpBQ74ufDKzRn7vXEBjiar+hSGTFZfg@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=us-ascii
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host adsl-76-249-20-110.dsl.klmzmi.sbcglobal.net
X-Newsreader Forte Agent 6.00/32.1186
X-No-Archive YES
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2519.1384306817.18130.python-list@python.org> (permalink)
Lines 117
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1384306817 news.xs4all.nl 15894 [2001:888:2000:d::a6]:36521
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:59274

Show key headers only | View raw


On Tue, 12 Nov 2013 09:44:51 +0530, "Lakshmipathi.G"
<lakshmipathi.g@gmail.com> declaimed the following:

>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

	Where is the description of the BDB write? What is the key, what is the
data...



>
>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")
>
	Looks normal for the representation of a binary "string"

>>> import struct
>>> a = "\x10'\x00\x00\x00\x00\x00\x00"
>>> a
"\x10'\x00\x00\x00\x00\x00\x00"
>>> struct.unpack("Q", a)
(10000,)

	Looks like your "e"... Is that the key field?

>>> b = "\xf4\x01\x00\x00\xd0;G\x00\x00\x00\x00\x00\x10'\x00\x00\x00\x00\x00\x00/home/laks/abcde\x00OSr\x00"
>>> b
"\xf4\x01\x00\x00\xd0;G\x00\x00\x00\x00\x00\x10'\x00\x00\x00\x00\x00\x00/home/laks/abcde\x00OSr\x00"
>>> struct.unpack("<lQ", b[:12])
(500, 4668368)


	That looks like your "o" and "i", but note that I had to specify
little-endian packed data... AND the order of the items is "o" before "i"
-- that doesn't seem to match your C struct definition.

>>> c = b[12:]
>>> c
"\x10'\x00\x00\x00\x00\x00\x00/home/laks/abcde\x00OSr\x00"
>>> struct.unpack("Q", c[:8])
(10000,)

	Hmmm, and here is your "e" value again... So far your structure appears
to be

o	32-bit int
i	64-bit uint
e	64-bit unit

NOT 

i	64 uint
e	64 uint
o	32 int

	Now comes the big problem... The struct module understands two types of
strings. "s" format in which you precode the length of the string in the
format ("10s" is a 10 character string), and "p" format in which the first
byte of the string is the length (0..255) of the rest of the string. The
struct module doesn't handle C-type null terminated strings directly.
	
>>> d = c[8:]
>>> d
'/home/laks/abcde\x00OSr\x00'
>>> n = d.index("\x00")
>>> n
16
>>> e = d[:n]
>>> e
'/home/laks/abcde'

	We've already got the string now, but to continue with struct module,
we'll use the above "n"

>>> s = struct.unpack("16s", e)
>>> s
('/home/laks/abcde',)

	And now to get the other one skipping the previous null termination

>>> f = d[n+1:]
>>> f
'OSr\x00'
>>> n2 = f.index("\x00")
>>> f[:n2]
'OSr'
>>> n2
3
>>> g = f[:n2]
>>> s2 = struct.unpack("3s", g)
>>> s2
('OSr',)
>>>

If you really want to do it in one struct.unpack, you'll need to modify
your C output structure to do fixed length strings without null termination
OR for variable length write the length as a byte and again drop the null
terminator.
 
-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
    wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Reading c struct via python Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-11-12 20:40 -0500

csiph-web