2

Now I am learning to analyse data in the BI tool QLikView. I tried to run a Python script while executing the QLikView script.

A part of my QLikView script where the Python script is called:

DatumRange: // maak tijdelijke tabellen aan met de datum range van de begin en eind datum per patiënt
LOAD DatumNum
Resident Kalender;

BeginEindVerblijf:
LOAD VerblijfStartNum, VerblijfEindNum
Resident Patiënt;

// sla de tabellen op in CSV bestanden om met behulp van een Python script om te zetten naar bezetting

STORE DatumRange INTO [C:\Users\masc\Desktop\Data-analyse voor U-Center in QLikView\Zelfgemaakte dummy dataset U-Center\Bezetting berekenen in Python\NumDateRange.csv] (txt);
STORE BeginEindVerblijf INTO [C:\Users\masc\Desktop\Data-analyse voor U-Center in QLikView\Zelfgemaakte dummy dataset U-Center\Bezetting berekenen in Python\VerblijfStart_VerblijfEind.csv] (txt);

DROP Table DatumRange;
DROP Table BeginEindVerblijf;

EXECUTE python3 BerekenBezetting.py // voer Python script uit om de bezetting te bepalen

Directory;
LOAD DatumNum, Date(DatumNum, 'YYYYMMDD') as JaarMaandDag, AantalPatiënten
FROM [Bezetting berekenen in Python\Bezetting.csv]
(txt, codepage is 1252, embedded labels, delimiter is ',', msq);

When executing the script I received the error:

General error
EXECUTE python3 BerekenBezetting.py 

Directory

In my QLikView log file I get the error:

2017-11-01 14:16:02 0303 STORE DatumRange INTO [C:\Users\masc\Desktop\Data-analyse voor U-Center in QLikView\Zelfgemaakte dummy dataset U-Center\Bezetting berekenen in Python\NumDateRange.csv] (txt)
2017-11-01 14:16:02 0304 STORE BeginEindVerblijf INTO [C:\Users\masc\Desktop\Data-analyse voor U-Center in QLikView\Zelfgemaakte dummy dataset U-Center\Bezetting berekenen in Python\VerblijfStart_VerblijfEind.csv] (txt)
2017-11-01 14:16:02 0306 DROP Table DatumRange
2017-11-01 14:16:02 0307 DROP Table BeginEindVerblijf
2017-11-01 14:16:02 0309 EXECUTE python3 BerekenBezetting.py 
2017-11-01 14:16:02 0310 
2017-11-01 14:16:02 0311 Directory
2017-11-01 14:16:02      Error: 
2017-11-01 14:16:09      Execution Failed
2017-11-01 14:16:09      Execution finished.

If I run my Python script in Python IDLE it works correctly and creates a .CSV file that is needed to import in QLikView after my Python script executed.

My Python script is:

NumDateRangeArray = [] # 1D array met alle numDate tussen de eerste en laatste datum
VerblijfStart_VerblijfEindArray = [] # 2D array waarin elke begin en einddatum opgeslagen wordt
bezetting = [] # 2D array waarin elke numDate in opgeslagen is tesamen met het aantal patiënt dat aanwezig is per dag

def main():

    bestandenInladen()
    bezettingBerekenen()
    maakCSVBestand()

def bestandenInladen():

    NumDateRangeFile = open('NumDateRange.csv' ,'r')
    VerblijfStart_VerblijfEindFile = open('VerblijfStart_VerblijfEind.csv', 'r')

    beginEind = []

    begin = ''
    eind = ''

    for date in NumDateRangeFile:
        if date.startswith(tuple(str(i) for i in range(10))):
            NumDateRangeArray.append(int(date.strip('\n'))) # verwijder de eerste niet nummerieke waarde

    for verblijf in VerblijfStart_VerblijfEindFile: # maak 2D array aan met begin en eind verblijf per patiënt
        beginEind = (verblijf.strip('\n').split(','))
        if beginEind[0].startswith(tuple(str(i) for i in range(10))):
            begin = int(beginEind[0])
            eind = int(beginEind[1])
            beginEind = [] # subarray met een begin en eindverblijf in NumDate formaat
            beginEind.append(begin)
            beginEind.append(eind)
            VerblijfStart_VerblijfEindArray.append(beginEind)

    NumDateRangeFile.close()
    VerblijfStart_VerblijfEindFile.close()

    #print (NumDateRangeArray)
    #print
    #print (VerblijfStart_VerblijfEindArray)

def bezettingBerekenen():

    aantalPatiëntenAanwezig = 0
    numDate = 0
    numDateAndAanwezigArray = [] # subarray met hoeveel patiënten er aanwezig zijn per datum

    for numDate in NumDateRangeArray:
        for verblijf in VerblijfStart_VerblijfEindArray:
            if numDate >= verblijf[0] and numDate <= verblijf[1]:
                aantalPatiëntenAanwezig += 1
        numDateAndAanwezigArray.append(numDate)
        numDateAndAanwezigArray.append(aantalPatiëntenAanwezig)
        bezetting.append(numDateAndAanwezigArray)
        numDateAndAanwezigArray = []
        aantalPatiëntenAanwezig = 0
    #print (bezetting)

def maakCSVBestand():

    import csv
    delimiter = ','
    csvfile = open('Bezetting.csv', 'w')
    csvfile.write('DatumNum' + delimiter + 'AantalPatiënten' + '\n')
    for datum in bezetting:
        csvfile.write(str(datum[0]) + delimiter + str(datum[1]) + '\n')
    csvfile.close()

main()

Whats going wrong here because the error message doesn't give me any information?

Mark Schuurman
  • 687
  • 1
  • 11
  • 25

2 Answers2

3

You need to include the full path to python.exe.

EXECUTE C:\Python27\python.exe c:\Users\User123\Desktop\test.py;

Also Script (Allow Database Write and Execute Statement) check box in User Preferences -> Security should be checked

enter image description here

Bear in mind that this setting is in User Preferences. Which means that it will be enabled only for you. If you share the file with someone else they will be unable to execute external files unles they have the same checkbox enabled

Stefan Stoichev
  • 4,615
  • 3
  • 31
  • 51
  • Thanks for your answer. I changed the line to execute my script into : EXECUTE C:\Users\masc\AppData\Local\Programs\Python\Python36\python.exe C:\Users\masc\Desktop\Data-analyse voor U-Center in QLikView\Zelfgemaakte dummy dataset U-Center\Bezetting berekenen in Python\BerekenBezetting.py; But it sill doesn't work. If I run the script in IDLE and create the file Bezetting.csv, QLikView runs without error but if I delete Bezetting.csv I get an error that the file couldn't be found. If not present the file must be re-created by running QLikView that executes the script. – Mark Schuurman Nov 01 '17 at 15:04
  • Try to wrap the path in `"` since you have spaces in the path. For example `EXECUTE C:\Users\masc\AppData\Local\Programs\Python\Python36\python.‌​exe "C:\Users\masc\Desktop\Data-analyse voor U-Center in QLikView\Zelfgemaakte dummy dataset U-Center\Bezetting berekenen in Python\BerekenBezetting.py"` – Stefan Stoichev Nov 01 '17 at 15:11
  • As far as i know the `EXECUTE` statement just fires `CMD` and passes the statement params. So if you can't run your command in cmd then you will not be able to run it from QV as well – Stefan Stoichev Nov 01 '17 at 15:12
  • I changed the command to : C:\\Users\\masc\\AppData\\Local\\Programs\\Python\\Python36\\python "C:\\Users\\masc\\Desktop\\Data-analyse voor U-Center in QLikView\\Zelfgemaakte dummy dataset U-Center\\Bezetting berekenen in Python\\BerekenBezetting.py" that works correctly in CMD but I still heve the same problem in QLikView. – Mark Schuurman Nov 02 '17 at 10:08
  • actually i can see that `;` is missing after the `EXECUTE` statement. Should be `EXECUTE python3 BerekenBezetting.py;` – Stefan Stoichev Nov 02 '17 at 11:06
1

Thanks to Stefan I found the solution, the command:

EXECUTE C:\\Users\\masc\\AppData\\Local\\Programs\\Python\\Python36\\python "C:\\Users\\masc\\Desktop\\Data-analyse voor U-Center in QLikView\\Zelfgemaakte dummy dataset U-Center\\Bezetting berekenen in Python\\BerekenBezetting.py";

works properly.

Mark Schuurman
  • 687
  • 1
  • 11
  • 25