[Tutor] reading output from a c executable.

Steve Willoughby steve at alchemy.com
Thu Dec 11 19:44:52 CET 2008


Ravi Kondamuru wrote:
> I am expecting these lists to be huge and was hoping to avoid re-parsing 
> in python. Any way for the c program to return a list that python can 

Mind if I ask the obvious question here?  Why are you wanting to avoid 
parsing in Python?  Any time you have one program (in any language) 
leave data behind for another program to consume, you're going to have 
some level of interpretation on the data, whether you're reading raw 
bytes and assuming you know how to put them together again or parsing 
ASCII characters or whatever.

Try something simple first and see if you get reasonable performance 
from it before you get too paranoid about avoiding something that might 
not be where you need to focus your attention.  Optimizing too early in 
the development effort often leads to wasted time on the things that 
surprisingly don't contribute to the real performance issues in a program.

If you're output is binary data (like a stream of bytes or words), 
Python can read that with a small bit of code to put those bytes into 
integer values in a list.  I'd suggest something like CSV as a first 
try, though, since that way the data will be intelligible for debugging 
purposes, leaves the possibility open for other scripts to read and 
alter the data too, and is very simple for Python to read via standard 
library modules too.

> directly use.
> Thanks for the pointer to json :) I am going to explore and evaluate 
> re-parsing overhead.
> thanks,
> Ravi.
> 
> On Thu, Dec 11, 2008 at 10:19 AM, bob gailer <bgailer at gmail.com 
> <mailto:bgailer at gmail.com>> wrote:
> 
>     Ravi Kondamuru wrote:
> 
>         Hi,
>         I am writing a script to read list output from a C executable.
>         How should c program be written so that python can read the
>         output as a list?
>         Any pointers to info on this appreciated.
> 
> 
>     Funny that a C programmer is asking for pointers, when C has lots of
>     pointers and Python has none. <chuckle>
> 
>     Python I/O reads strings. So you must encode the list in some manner
>     into a string and decode it into a list in the Python program.
> 
>     Take a look as json. http://www.json.org/
> 
>     There are links to C and Python. You should be able there to
>     findThats how to encode in C and decode in Python.
> 
>     You could "roll your own" encoding" but I think json would be easier.
> 
>     Remember to reply-all so a copy goes to the list.
> 
>     -- 
>     Bob Gailer
>     Chapel Hill NC 919-636-4239
> 
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list