[Tutor] ctypes and spssio.dll
Albert-Jan Roskam
fomcl at yahoo.com
Wed Mar 16 21:24:16 CET 2011
Hi,
I'm still working on a program that uses a .dll to read SPSS system files.
It's getting somewhere already, but I'm still struggling with one thing.
When using the C function spssGetVarNames I'm having trouble
translating C arrays to Python lists. I want to translate an
array that holds the variable names into a Python list. Same thing
for variable types.
The documentation of the SPSSIO dll says the following:
spssGetVarNames
int spssGetVarNames (int handle, int *numVars, char ***varNames, int **varTypes)
Parameter - Description
handle - Handle to the data file
numVars - Pointer to number of variables
varNames - Pointer to array of pointers to variable
varTypes - Pointer to array of variable types
In the code below, which can also be found on http://pastebin.com/d7d0hpyV,
the equivalent Python function is called getVarNamesAndTypes(). This is
the output I typically get:
retcode: 0
Varnames: ['\x80\xde\x10\x01P\xd8\x10\x01\xf0\xd0\x10\x01', None, None, None,
None, None, None, None, None, None]
varTypes: [17885264, 0, 0, 0, 0, 0, 0, 0, 0, 0]
The first item of each list is clearly wrong, but what does it mean? If varType
> 0 it is
supposed to be a string var of that length. And of course the varNames are
mostly 'None'.
Probably, something is wrong with the way the C arrays are initialized,
or with the two list comprehensions, but I can't find the solution.
varNames = [varNamesPtr[0][i] for i in range(numVars)] does not seem to work.
For those who are interested, the complete program can be found on
http://pastebin.com/ff1b1Y9a (note that it's still work in progress)
Any hints? Thanks very much in advance!
import os, ctypes, datetime
# dll and py file should be in the same dir!
def loadSavFile(fn):
os.environ["PATH"] += ";" + os.path.abspath(os.curdir)
ctypes.cdll.LoadLibrary("spssio32.dll")
spssio = ctypes.windll.spssio32
libc = ctypes.cdll.msvcrt
if os.path.exists(fn):
fh = libc._fdopen(fn, "rb")
fhPtr = ctypes.pointer(ctypes.c_int(fh))
retcode = spssio.spssOpenRead(ctypes.c_char_p(fn), fhPtr)
return retcode, spssio, fh
else:
raise Exception, "File '%s' does not exist!" % fn
def getVarNamesAndTypes(fh, spssio):
numVarsPtr = ctypes.pointer(ctypes.c_int())
spssio.spssGetNumberofVariables(fh, numVarsPtr)
numVars = numVarsPtr[0]
varNamesArray = (ctypes.c_char_p * numVars)()
varNamesPtr = ctypes.pointer(varNamesArray)
varTypesArray = (ctypes.c_int * numVars)()
varTypesPtr = ctypes.pointer(varTypesArray)
retcode = spssio.spssGetVarNames(fh, numVarsPtr, varNamesPtr, varTypesPtr)
varNames = [varNamesPtr[0][i] for i in range(numVars)] # -- WRONG!!
varTypes = [varTypesPtr[0][i] for i in range(numVars)]
return retcode, varNames, varTypes
savFileName = r"C:\Program Files\SPSS\Employee data.sav"
retcode, spssio, fh = loadSavFile(savFileName)
retcode, varNames, varTypes = getVarNamesAndTypes(fh, spssio)
print "retcode:", retcode
print "Varnames:", varNames
Cheers!!
Albert-Jan
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All right, but apart from the sanitation, the medicine, education, wine, public
order, irrigation, roads, a fresh water system, and public health, what have the
Romans ever done for us?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110316/fbf9b8ed/attachment-0001.html>
More information about the Tutor
mailing list