2

I have a file.cc that contains an array of doubles values, as seen here:

double values[][4] = {
  { 0.1234, +0.5678, 0.1222, 0.9683 },
  { 0.1631, +0.4678, 0.2122, 0.6643 },
  { 0.1332, +0.5678, 0.1322, 0.1683 },
  { 0.1636, +0.7678, 0.7122, 0.6283 }
  ... continue
}

How can I export these values to a Python list?

I cannot touch these files because they belong to an external library, subject to modification. Exactly, I want to be able to update the library without affecting my code.

3
  • 2
    Depends on the application: is this just a one-off thing where you need to move those numbers over to Python and keep them there, or do you need to access them from running C code, etc. Just converting the text from C to Python should be trivial with a little Python script. Accessing them at runtime is another can or worms. Commented May 13, 2013 at 2:42
  • This file.cc should be some kind of C library to be used by a python program or an independent program? In case A you could check for ctypes, cython or write a small python C extension; in the second case you simply have to use some kind of IPC(exactly as if the python program was a different C program). Commented May 13, 2013 at 7:50
  • I cannot touch these files because they belong to an external library, subject to modification. Exactly, I want to be able to update the library without affecting the rest of my code. Commented May 13, 2013 at 11:57

2 Answers 2

2

This is pretty much answered in this other SO post.

But I will add a bit here. You need to define a type then use the in_dll method.

From your example I made a so with those values in values. I hope you have an idea how big it is or can find out from other vars in the library, otherwise this is a seg fault waiting to happen.

import ctypes
lib = ctypes.CDLL('so.so')
da = ctypes.c_double*4*4
da.in_dll(lib, "values")[0][0]
# 0.1234
da.in_dll(lib, "values")[0][1]
# 0.5678
da.in_dll(lib, "values")[0][2]
# 0.1222

From here I would just loop over them reading into a list.

Sign up to request clarification or add additional context in comments.

1 Comment

Good to hear. Unless a better answer appears you should accept the answer that works for you.
0

How about using a temporary file? Put the matrix in it by C and read them by python.

In file.cc, write a function to save the matrix to a file.

int save_to_file(double matrix[][4],int row) {
    int i,j;
    FILE *fp;
    fp=fopen("tmp","w");
    for(i=0;i<row;i++)
        for(j=0;j<4;j++) {
            fprintf(fp,"%f",matrix[i][j]);
            if(j==3)
                fprintf(fp,"\n",matrix[i][j]);
            else
                fprintf(fp," ",matrix[i][j]);
        }
    fclose(fp);
    return 0;
}

and read them by a Python script like this:

tmp=open('tmp')
L = []
for line in tmp:
    newline = []
    t = line.split(' ')
    for string in t:
        newline.append(float(string))
    L.append(newline)
tmp.close()

for row in L:
    for number in row:
        print "%.4f" %number
    print " "

1 Comment

It could work, however I would like to do something more automatic with ctypes. I tried to use h2xml.py and xml2py.py but the last one doesn't seem to support that the array be initialized. It returns with "Cannot initialize ArrayType" error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.