Problems returning data from embedded Python

Gabriel Genellina gagsl-py2 at
Tue Aug 12 06:37:53 CEST 2008

En Mon, 11 Aug 2008 12:58:00 -0300, Cromulent  
<cromulent at> escribi�:

> Okay I'm having a few issues with this and I can't seem to get it sorted  
> out (most likely due to my inexperience with Python).
> Here is my Python code:
> def fileInput():
> 	data = []
> 	s = raw_input("Please enter the filename to process (enter full path if  
> not in current directory): ")

(in general, it's not a good idea mixing the user interfase with data  
processing - I'd make the filename a parameter for this function. But this  
has nothing to do with your main issue.)

> 	fd = open(s, "r")
> 	fd.readline()
> 	line = fd.readlines()
> 	for record in line:

I'd replace the above two lines with:
         for record in fd:

> 		record = record.strip()
> 		items = record.split(',')
> 		for i in range(1, 5):
> 			items[i] = float(items[i])
> 		items[5] = int(items[5])
> 		data.append(items)
> 	fd.close()
> 	return items

`return items` or `return data`? Are you only interested on the *last*  
line in the file? If so, I'd skip processing all previous lines...

> It works perfectly and does exactly what I want it to do. The problem  
> comes when I try and convert the data I returned from the Python script  
> and turn it into something useable in C.
> I have no idea. The C functions that seem most use deal with Tuples,  
> when this is a list and I can't see any specific functions for dealing  
> with lists in the C API. Can anyone give me some pointers in the right  
> direction at all please? I know the C program has received the data  
> correctly, I just need to put it in C types.

Uh? You have a complete API for working with list objects, the functions  
named PyList_*
There is also an abstract layer that works both with lists and tuples:
If that's not what you are after, please provide more details...

Gabriel Genellina

More information about the Python-list mailing list