Can Python read .sps files created by versions 16+?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Can Python read .sps files created by versions 16+?

Ruben Geert van den Berg
Dear all,
 
I wanted to identify all .sps files in my folder and subfolders that contain any HOST command. My program does render a couple of .sps files which all contain at least one HOST command. However, it seems as if these are all syntax files created by version 15 or older (which are readable with notepad, whereas 16+ seems to require wordpad). For the sake of the experiment, I created a little syntax file with a HOST command in V17 and it was -indeed- not identified as an .sps file containing a HOST command.
 
Could anyone suggest how I could modify my program to include version 16+ syntax as well?
 
TIA!

Ruben van den Berg
Consultant Models & Methods
TNS NIPO
Email: [hidden email]
Mobiel: +31 6 24641435
Telefoon: +31 20 522 5738
Internet: www.tns-nipo.com

begin program.
import os
def app(path):
    paths = []
    for dp, ds, fs in os.walk(path):
        for f in fs:
            paths.append(os.path.join(dp,f))
    return paths
end program.
 
begin program.
paths=app('/Copy_Syn')
targets=[]
for path in paths:
    file=open(path,"r")
    for line in file:
        if "HOST".lower() in line:
            targets.append(path)
print "\n".join(targets)
end program.


New Windows 7: Find the right PC for you. Learn more.
Reply | Threaded
Open this post in threaded view
|

Re: Can Python read .sps files created by versions 16+?

Jon K Peck

V16 files are no different in format from earlier syntax files, although they could be in Unicode.  Unicode would not affect recognition of plain ascii text such as HOST, though.

But the code below lower cases "HOST" and then searches each line for "host".  I'd do it the other way around: lower case line and search for "host"

Also, note that os.walk is designed to return files one by one, so you could do the searching within the loop in the first program.

HTH,

Jon Peck
SPSS, an IBM Company
[hidden email]
312-651-3435



From: Ruben van den Berg <[hidden email]>
To: [hidden email]
Date: 07/23/2010 03:24 AM
Subject: [SPSSX-L] Can Python read .sps files created by versions 16+?
Sent by: "SPSSX(r) Discussion" <[hidden email]>





Dear all,

I wanted to identify all .sps files in my folder and subfolders that contain any HOST command. My program does render a couple of .sps files which all contain at least one HOST command. However, it seems as if these are all syntax files created by version 15 or older (which are readable with notepad, whereas 16+ seems to require wordpad). For the sake of the experiment, I created a little syntax file with a HOST command in V17 and it was -indeed- not identified as an .sps file containing a HOST command.

Could anyone suggest how I could modify my program to include version 16+ syntax as well?

TIA!

Ruben van den Berg

Consultant Models & Methods

TNS NIPO

Email: [hidden email]

Mobiel: +31 6 24641435

Telefoon: +31 20 522 5738

Internet:
www.tns-nipo.com

begin program.
import os
def app(path):
   paths = []
   for dp, ds, fs in os.walk(path):
       for f in fs:
           paths.append(os.path.join(dp,f))
   return paths
end program.

begin program.
paths=app('/Copy_Syn')
targets=[]
for path in paths:
   file=open(path,"r")
   for line in file:
       if "HOST".lower() in line:
           targets.append(path)
print "\n".join(targets)
end program.


New Windows 7: Find the right PC for you. Learn more.

Reply | Threaded
Open this post in threaded view
|

SOLVED: [SPSSX-L] Can Python read .sps files created by versions 16+?

Ruben Geert van den Berg
Oh, I see! The problem is that
 
if "HOST".lower() in line:
 
should have been
 
if "host" in line.lower():
 
And now it indeed renders my V17 test file as well! Well, at least I seem to have a talent for looking in the wrong places when my code doesn't work...
 
Thanks a lot for helping out again!

Ruben van den Berg
Consultant Models & Methods
TNS NIPO
Email: [hidden email]
Mobiel: +31 6 24641435
Telefoon: +31 20 522 5738
Internet: www.tns-nipo.com



 

To: [hidden email]
CC: [hidden email]
Subject: Re: [SPSSX-L] Can Python read .sps files created by versions 16+?
From: [hidden email]
Date: Fri, 23 Jul 2010 06:54:38 -0600


V16 files are no different in format from earlier syntax files, although they could be in Unicode.  Unicode would not affect recognition of plain ascii text such as HOST, though.

But the code below lower cases "HOST" and then searches each line for "host".  I'd do it the other way around: lower case line and search for "host"

Also, note that os.walk is designed to return files one by one, so you could do the searching within the loop in the first program.

HTH,

Jon Peck
SPSS, an IBM Company
[hidden email]
312-651-3435



From: Ruben van den Berg <[hidden email]>
To: [hidden email]
Date: 07/23/2010 03:24 AM
Subject: [SPSSX-L] Can Python read .sps files created by versions 16+?
Sent by: "SPSSX(r) Discussion" <[hidden email]>





Dear all,

I wanted to identify all .sps files in my folder and subfolders that contain any HOST command. My program does render a couple of .sps files which all contain at least one HOST command. However, it seems as if these are all syntax files created by version 15 or older (which are readable with notepad, whereas 16+ seems to require wordpad). For the sake of the experiment, I created a little syntax file with a HOST command in V17 and it was -indeed- not identified as an .sps file containing a HOST command.

Could anyone suggest how I could modify my program to include version 16+ syntax as well?

TIA!

Ruben van den Berg

Consultant Models & Methods

TNS NIPO

Email: [hidden email]

Mobiel: +31 6 24641435

Telefoon: +31 20 522 5738

Internet:
www.tns-nipo.com

begin program.
import os
def app(path):
   paths = []
   for dp, ds, fs in os.walk(path):
       for f in fs:
           paths.append(os.path.join(dp,f))
   return paths
end program.

begin program.
paths=app('/Copy_Syn')
targets=[]
for path in paths:
   file=open(path,"r")
   for line in file:
       if "HOST".lower() in line:
           targets.append(path)
print "\n".join(targets)
end program.


New Windows 7: Find the right PC for you. Learn more.



New Windows 7: Find the right PC for you. Learn more.
Reply | Threaded
Open this post in threaded view
|

Re: SOLVED: [SPSSX-L] Can Python read .sps files created by versions 16+?

Albert-Jan Roskam
Hi Ruben,

If you put a 'break' in the inner loop you might speed up your program a bit. The inner loop is terminated right when the first HOST command is found, ie no time wasted on the remainder of the file.

I probably doesn't matter much in this case, but still ;-)

Cheers!!
Albert-Jan

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

--- On Fri, 7/23/10, Ruben van den Berg <[hidden email]> wrote:

From: Ruben van den Berg <[hidden email]>
Subject: [SPSSX-L] SOLVED: [SPSSX-L] Can Python read .sps files created by versions 16+?
To: [hidden email]
Date: Friday, July 23, 2010, 3:21 PM

Oh, I see! The problem is that
 
if "HOST".lower() in line:
 
should have been
 
if "host" in line.lower():
 
And now it indeed renders my V17 test file as well! Well, at least I seem to have a talent for looking in the wrong places when my code doesn't work...
 
Thanks a lot for helping out again!

Ruben van den Berg
Consultant Models & Methods
TNS NIPO
Email: [hidden email]
Mobiel: +31 6 24641435
Telefoon: +31 20 522 5738
Internet: www.tns-nipo.com



 

To: [hidden email]
CC: [hidden email]
Subject: Re: [SPSSX-L] Can Python read .sps files created by versions 16+?
From: [hidden email]
Date: Fri, 23 Jul 2010 06:54:38 -0600


V16 files are no different in format from earlier syntax files, although they could be in Unicode.  Unicode would not affect recognition of plain ascii text such as HOST, though.

But the code below lower cases "HOST" and then searches each line for "host".  I'd do it the other way around: lower case line and search for "host"

Also, note that os.walk is designed to return files one by one, so you could do the searching within the loop in the first program.

HTH,

Jon Peck
SPSS, an IBM Company
[hidden email]
312-651-3435



From: Ruben van den Berg <[hidden email]>
To: [hidden email]
Date: 07/23/2010 03:24 AM
Subject: [SPSSX-L] Can Python read .sps files created by versions 16+?
Sent by: "SPSSX(r) Discussion" <[hidden email]>





Dear all,

I wanted to identify all .sps files in my folder and subfolders that contain any HOST command. My program does render a couple of .sps files which all contain at least one HOST command. However, it seems as if these are all syntax files created by version 15 or older (which are readable with notepad, whereas 16+ seems to require wordpad). For the sake of the experiment, I created a little syntax file with a HOST command in V17 and it was -indeed- not identified as an .sps file containing a HOST command.

Could anyone suggest how I could modify my program to include version 16+ syntax as well?

TIA!

Ruben van den Berg

Consultant Models & Methods

TNS NIPO

Email: [hidden email]

Mobiel: +31 6 24641435

Telefoon: +31 20 522 5738

Internet:
www.tns-nipo.com

begin program.
import os
def app(path):
   paths = []
   for dp, ds, fs in os.walk(path):
       for f in fs:
           paths.append(os.path.join(dp,f))
   return paths
end program.

begin program.
paths=app('/Copy_Syn')
targets=[]
for path in paths:
   file=open(path,"r")
   for line in file:
       if "HOST".lower() in line:
           targets.append(path)
print "\n".join(targets)
end program.


New Windows 7: Find the right PC for you. Learn more.



New Windows 7: Find the right PC for you. Learn more.