Question

I'm trying to create a very simple stand-alone app that converts CATIA .dat files into csv for ProCast.

I have a Qtwidget File dialog to get the .dat file :

app = QtGui.QApplication(sys.argv)
widget = QtGui.QWidget()
widget.show()
DATFILE = QtGui.QFileDialog.getOpenFileName(widget, 'Open File', '.')
NODES, ELEMENTS, CONNECT = read(DATFILE)

To load the data from the .dat file I first use with open(file) and a while loop and then np.genfromtxt for the rest of the file like so :

def read(infile):
    with open(infile, 'r') as inf:
        line = inf.readline()
        while "NODES" not in line:
            line = inf.readline()
        inf.readline()
        line = inf.readline()
        list_nodes = []
        while '$' not in line:
            x, y = line.split()[2:4]
            z = inf.readline().split()[2]
            list_nodes.append([float(x), float(y), float(z)])
            line = inf.readline()
    num_nodes = len(list_nodes)
    nodes = np.zeros((num_nodes, 4))
    nodes[:, 0] = np.arange(1, num_nodes+1)
    for n in range(len(list_nodes)):
        nodes[n, 1:] = np.fromiter(list_nodes[n], dtype=float)
skipheader = np.size(nodes, axis=0)*2+12
elements = np.genfromtxt(infile, dtype=int, comments='$', skip_footer=1,
                         skip_header=skiph, usecols=(3,4,5))

When I run the my read function with a infile argument as a string I type, it works perfectly, but when I try to use the filepath that PyQt File dialog gave me, numpy.genfromtxt fails :

Traceback (most recent call last):
File "E:\Felix\PJE\BOLOS\bolos.py", line 62, in <module>
NODES, ELEMENTS, CONNECT = lire(DATFILE)
File "E:\Felix\PJE\BOLOS\bolos.py", line 36, in lire
skip_header=skiph, usecols=(3,4,5))
File "C:\Python27\lib\site-packages\numpy\lib\npyio.py", line 1265, in genfromtxt
fhd.next()
StopIteration

from what I read in npyio.py the error occurs when it skips the lines I specified with skip_header, but I have no idea why it would fail now and not when I input the filename manually, since the other part of the read function works OK.

Also if it is a filename problem, wouldn't the error be raised earlier ? Can anyone see the (probably stupid and gross) mistake I made ?

I'm running python 2.7, Numpy 1.6.1 on windows 7.

Was it helpful?

Solution

QFileDialog.getOpenFileName() returns a QString that you're passing to your function. If it works fine when you pass the path as a string you type I'm guessing it's a Python string that you're entering?

Try converting the QString to a Python string and see if that fixes things.

DATFILE = str(QtGui.QFileDialog.getOpenFileName(widget, 'Open File', '.'))

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top