Error when trying to perform IDEAScript extraction in Python
Forums
I have an IDEA file from which I want to perform a simple extraction (column1 equals 6) and save this as a new file. My IDEAScript is as follows (see also attached file):
Sub Main
Call DirectExtraction()
End Sub
Function DirectExtraction
Set db = Client.OpenDatabase("myfile.IMD")
Set task = db.Extraction
task.IncludeAllFields
dbName = "outputfile.IMD"
task.AddExtraction dbName, "", column1 == ""6"""
task.PerformTask 1, db.Count
Set task = Nothing
Set db = Nothing
Client.OpenDatabase (dbName)
End Function
This works.
However, I now want to do this from Python using win32com.client. So I tried (the os.path.join works and the file exists):
import win32com.client as win3ComClient
try:
idea = win32ComClient.Dispatch(dispatch="Idea.IdeaClient")
db = idea.OpenDatabase(os.path.join(wd, os.path.basename(prefix)+'_myfile.IMD'))
task = db.Extraction
task.IncludeAllFields
dbName = os.path.join(wd, os.path.basename(prefix)+'_outputfile.IMD')
task.AddExtraction(dbName, "", "Column1 == ""6""")
task.PerformTask(1, db.Count)
task = Nothing
db = Nothing
finally:
idea = None
However, this gives me the error AttributeError: 'function' object has no attribute 'IncludeAllFields' and it marks the line with task.IncludeAllFields. So where is the mistake? So it seems like I did not "translate" the IDEAScript VB properly to Python style. The function db.Extraction does not have the attribute IncludeAllFields. But in IDEAScript it does. How can I get this running? I also tried to write it in one line, so db.Extraction.IncludeAllFields but still I get an error. So I don't know how to configure the task in general using Python. This seems to be the source of the error.
Hi Brian,
Hi Brian,
first of all a big thanks for the fast answer and this hint! Good to know. I know changed the two lines to:
task = db.Extraction()
task.IncludeAllFields()
It throws no error. However, I not get an error which marks the line
task.PerformTask(1, db.Count) and says that the equation I gave is not correct. However, it is correct, I took it from the IDEAScript. I also tried to play around with the quotation marks, so I tried "Column1 == 6", but still it doesn't work. Where is the mistake, why does it not work? I also tried to add () to the db.Count, so task.PerformTask(1, db.Count()), but I get an error TypeError: 'float' object is not callable.
When the perform task gives
When the perform task gives an error that doesn't mean that there is aproblem on that line but it could be any. Your guess is correct that it is probably the criteria. Python doesn't allow the two double quotes when writing the equation so you have two options. The first is to surround it with a single quote, so your criteria becomes:
'Column1 == "6"'
or you can use slash to indicate a special character
"Column1 == /"6/""
I think it is the forward slash but it might be the backward one, I can't remember at the moment.
I am assuming the Column1 is a character field. Also if it is a field name it should be COLUMN1, sometimes when changing it to uppercase it might have changes that you aren't expecting. So make sure your field names match.
Dear Brian, thanks a lot, it
Thanks Brian for the help. The code itself now seems to work, at least it starts and IDEA applies the criteria and starts the extraction. But after the criteria is applied and the extraction is done I get the error message that "the new information cannot be written to the output file". I don' know what the reason is. I thought maybe I don't have enough disk space, but this is not the problem.
The error is an IDEA error. I
The error is an IDEA error. I can see IDEA applying the criteria and performing the extraction. And even the file gets created, however at the end IDEA somehow tries to add additional information and this fails. When I create the file just with IDEA manually I get the same number of rows, but the size is minimal smaller.
Sorry, the mistake was on my
Sorry, the mistake was on my side. It now works (with both Nothing and None, but I will take None). I somehow "distrupted" the file before with simple copying it. I did get a warning from IDEA that files of the same ID exists. I removed one, but somehow there must have been a mistake here.
When I now check it with a new folder it works.
So big thanks for your ongoing support!
That is one of the quirks of
That is one of the quirks of IDEASCript to python, you often have to add the () to the functions.
In your case you need to add () to task.IncludeAllFields so it becomes task.IncludeAllFields(). Also you will probalby have to add () on the db.Extraction line.