All, I am writing some python code with the spss module to add
cases. I have 38 files that need to be merged using add cases
logic. My code goes to the directory gets the files paths and creates a tuple
of them. As I open each file I give each a name rxFile1 thru rxFile38. I want
to be able to assign each a name so that I can then loop through the tuple activating
the appropriate files by name and combining them. I’m starting with
file1, adding file2 then saving file1 (with file2 now added) then adding file3
to the new file1 etc. The error I get is Expecting dataset name or WINDOW. Found rxfile1. Whatever I call the dataset name (here called rxfile1) I get
this error. I was originally just going to use the full file name, instead of
having to create these new filenames. It works fine with the file name, ‘.sav’
and all in Statistics. What is going on here? I’ve used dataset name
explicitly providing the name in python before. This is the first time I’m
trying to dynamically assign the name. Any help, much appreciated! Here’s my code: import spss, os, glob path = 'D:\\Data\\RxAmerica\\test' fileList = () nameList = () fileNum = 0 for infile in glob.iglob( os.path.join(path, '*.sav') ): print "current file is: " +
infile fileNum += 1 fileName = 'rxfile' + str(fileNum) spss.Submit(r"""get file =
'%s'. dataset name '%s'.""" %
(infile, fileName)) fileList = fileList +
(infile,) nameList = nameList + (fileName,) month1 = 0 month2 = 1 while month2 < len(fileList)-1: spss.Submit(r""" DATASET ACTIVATE '%s'. ADD FILES /FILE=* /FILE='%s'. EXECUTE. save outfile = '%s'
/compressed.""") % nameList[month1], nameList[month2],
fileList[month1] month2 += 1 Matthew Pirritano, Ph.D. Research Analyst IV Medical Services Initiative (MSI) Orange County Health Care Agency (714) 568-5648 |
You are quoting the dataset name. Those
names should not be quoted. They have the same syntactic status as
variable names.
HTH, Jon Peck Senior Software Engineer, IBM [hidden email] 312-651-3435 From: "Pirritano, Matthew" <[hidden email]> To: [hidden email] Date: 02/11/2011 03:48 PM Subject: [SPSSX-L] dataset name not working in python Sent by: "SPSSX(r) Discussion" <[hidden email]> All, I am writing some python code with the spss module to add cases. I have 38 files that need to be merged using add cases logic. My code goes to the directory gets the files paths and creates a tuple of them. As I open each file I give each a name rxFile1 thru rxFile38. I want to be able to assign each a name so that I can then loop through the tuple activating the appropriate files by name and combining them. I’m starting with file1, adding file2 then saving file1 (with file2 now added) then adding file3 to the new file1 etc. The error I get is Expecting dataset name or WINDOW. Found rxfile1. Whatever I call the dataset name (here called rxfile1) I get this error. I was originally just going to use the full file name, instead of having to create these new filenames. It works fine with the file name, ‘.sav’ and all in Statistics. What is going on here? I’ve used dataset name explicitly providing the name in python before. This is the first time I’m trying to dynamically assign the name. Any help, much appreciated! Here’s my code: import spss, os, glob path = 'D:\\Data\\RxAmerica\\test' fileList = () nameList = () fileNum = 0 for infile in glob.iglob( os.path.join(path, '*.sav') ): print "current file is: " + infile fileNum += 1 fileName = 'rxfile' + str(fileNum) spss.Submit(r"""get file = '%s'. dataset name '%s'.""" % (infile, fileName)) fileList = fileList + (infile,) nameList = nameList + (fileName,) month1 = 0 month2 = 1 while month2 < len(fileList)-1: spss.Submit(r""" DATASET ACTIVATE '%s'. ADD FILES /FILE=* /FILE='%s'. EXECUTE. save outfile = '%s' /compressed.""") % nameList[month1], nameList[month2], fileList[month1] month2 += 1 Matthew Pirritano, Ph.D. Research Analyst IV Medical Services Initiative (MSI) Orange County Health Care Agency (714) 568-5648 |
Ahhh! Thank you. So simple! I didn’t
realize I did that. I’ve only used %s in the past for filenames so I got used
to putting it in quotes. Thanks Matt Matthew Pirritano, Ph.D. Research Analyst IV Medical Services Initiative (MSI) Orange County Health Care Agency (714) 568-5648 From: Jon K Peck
[mailto:[hidden email]] You are quoting the dataset name. Those names
should not be quoted. They have the same syntactic status as variable
names.
|
Free forum by Nabble | Edit this page |