0

Hello,

I'm a Python/SPSS (DANIWEB) beginner and I'm having a hard time getting classes, functions and methods into the right order. I want to write a script for SPSS where I can read text out of an text object from the SPSS-output file. My code looks like this:

import SpssClient                                                 

SpssClient.StartClient()

OutputDoc = SpssClient.GetDesignatedOutputDoc()                     
OutputItems = OutputDoc.GetOutputItems()
OutputDoc.ClearSelection()


for index in range(OutputItems.Size()):
    OutputItems = OutputItems.GetItemAt(index)
    text = OutputItems.GetTextContents()
    if text.find("  Number of cases read:  0    Number of cases listed:  0") >= 0:
        OutputItems.SetSelected(True)

SpssClient.StopClient()       
print "fertig!"
OutputDoc.Delete()

error Message:
Traceback (most recent call last):
File "\\io\userdaten\EPIDEMIOLOGIE\Matthias\- Skripte\DingeEntfernen.py", line 11, in <module>
text = OutputItems.GetTextContents()
AttributeError: 'SpssClient.SpssOutputItem' object has no attribute 'GetTextContents'

I know that the I'm not using the "SpssOutputItem" Class inside my code. Why is it inside the error message?
The "GetTextContents" method is inside the "SpssLogItem" Class. How do I call these classes in the right way? I studied all the instructions but I can't get it right...
Could anyone help me please?

3
Contributors
9
Replies
10
Views
6 Years
Discussion Span
Last Post by DerKleineDude
0

In this line
OutputItems = OutputItems.GetItemAt(index)
you are redefining OutputItems as a single output item, which, of course, then does not have a method GetItemAt.
Do it as
OutputItem = OutputItems.GetItemAt(index)
and then use GetTextContents on that.

HTH,
Jon Peck

0

I see what you meant. I double called the methods. I corrected other mistakes in that category, but it is still not working.

import SpssClient                                                 

SpssClient.StartClient()

OutputDoc = SpssClient.GetDesignatedOutputDoc()                     
OutputItems = OutputDoc.GetOutputItems()

for index in range(OutputItems.Size()):
    OutputItem = OutputItems.GetItemAt(index)
    text = OutputItem.GetTextContents()
    if text.find("  Number of cases read:  0    Number of cases listed:  0") >= 0:
        OutputItem.SetSelected(True)

SpssClient.StopClient()       
print "fertig!"
OutputDoc.Delete()

Error Message:
..., line 10, in <module>
text = OutputItem.GetTextContents()
AttributeError: 'SpssClient.SpssOutputItem' object has no attribute 'GetTextContents'

I still know "GetTextContents" is inside the "SpssLogItem" Class and not in "SpssOutputItem". Why is it returning such an error?!

0

Not all output items have the GetTextContents method, e.g., header items, so you need either to check the type before calling the method or, better, just use a try/except block to catch and ignore the exception and proceed to the next item.

0

Use test printing, put before line 10 as test:

print OutputItem,type(OutputItem)

Ok. I tested variable "OutputItem" by inserting "print". Before line 10 the output is:

<SpssClient.SpssOutputItem object at 0x01306360> <type 'SpssClient.SpssOutputItem'>

Traceback (most recent call last):
File "...test.py", line 11, in <module>
text = OutputItem.GetTextContents()
AttributeError: 'SpssClient.SpssOutputItem' object has no attribute 'GetTextContents'

I guess this leads to this:

Not all output items have the GetTextContents method, e.g., header items, so you need either to check the type before calling the method or, better, just use a try/except block to catch and ignore the exception and proceed to the next item.

"OutputItem" takes the first item which is a LOG (SPSS Log containing the information of the SPSS-Syntax).
Then it tries to compare the LOG with the text "...Number of cases read...". This leads to an error.

Am I right?

Thanks for the help so far. At the moment I'm trying to set up a try/except block.

0

Two more problems:

First, before you can call GetTextContents, you have to call GetSpecificType. For example,
OutputItem = OutputItems.GetItemAt(index).GetSpecificType()

Another minor problem, you need to call the Delete method BEFORE calling StopClient.

HTH,
Jon Peck

0

Two more problems:

First, before you can call GetTextContents, you have to call GetSpecificType. For example,
OutputItem = OutputItems.GetItemAt(index).GetSpecificType()

Another minor problem, you need to call the Delete method BEFORE calling StopClient.

HTH,
Jon Peck

I already fixed the "Delete" method in my version and all the other little things ;-)

At the start of this post I tried to call "GetSpecificType" but it led to:

AttributeError: 'SpssHeaderItem' object has no attribute 'GetTextContents'
I inserted it at the same place as you did.

0

OK. Here is an entire script (embedded as a program), that just prints whatever text it can find in the objects.

begin program.
import SpssClient
SpssClient.StartClient()
OutputDoc = SpssClient.GetDesignatedOutputDoc()
OutputItems = OutputDoc.GetOutputItems()
for index in range(OutputItems.Size()):
  try:
    OutputItem = OutputItems.GetItemAt(index).GetSpecificType()
    print index, OutputItem
    text = OutputItem.GetTextContents()
    print text
  except:
    pass
OutputDoc.Delete()
SpssClient.StopClient()
print "fertig!"
end program.
0

Thanks for the code. This was my major problem. Now that I know the "text" variable is filled right, I just need to read out the content and compare it with my string.

Thanks so far! I will publish the code when it's done. And if I fail, I'll write again. ;-)

Edited by DerKleineDude: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.