LISTSERV at the University of Georgia
Menubar Imagemap
Home Browse Manage Request Manuals Register
Previous messageNext messagePrevious in topicNext in topicPrevious by same authorNext by same authorPrevious page (August 2008)Back to main SPSSX-L pageJoin or leave SPSSX-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:         Tue, 19 Aug 2008 08:32:25 -0700
Reply-To:     fomcl@yahoo.com
Sender:       "SPSSX(r) Discussion" <SPSSX-L@LISTSERV.UGA.EDU>
From:         Albert-jan Roskam <fomcl@yahoo.com>
Subject:      Re: Appending with WRITE; String variables in quotes --PYTHON
Comments: To: Greg Chulsky <chulsky@bu.edu>
In-Reply-To:  <48A4872F.2030406@bu.edu>
Content-Type: text/plain; charset=us-ascii

Hi Greg,

The code below should do the trick. It adds/appends all files in a given dir and saves them as one combined file called 'merged.sav'. I borrowed some code from Jon Peck and modifid it so every time the ADD FILES maximum is reached, add files is executed. Perhaps this could be done in a simpler way, but at least it works!

Cheers!! Albert-Jan

*sample code to generate a bunch of files. BEGIN PROGRAM. import spss for myfile in range (1, 110): myfile2 = str(myfile).zfill(3) spss.Submit(""" input program. loop #sample=1 to 10. compute x = rv.normal(0,1). end case. end loop. end file. end input program. compute source = %s. """ % (myfile)) if myfile % 10 == 0 and myfile != 0: spss.Submit("save outfile = 'd:/temp/out%s.sav'." % (myfile2)) else: spss.Submit("xsave outfile = 'd:/temp/out%s.sav'." % (myfile2)) spss.Submit("exe.") END PROGRAM.

*actual code. set mprint = on.

BEGIN PROGRAM. import spss import glob savlist = glob.glob("d:/temp/out*.sav") savlist.sort() if savlist: mylen = len(savlist) for i in range (0, mylen, 50): j = i + 49 if i == 0: cmd = ["ADD FILES "] + ["/FILE='" + fn + "'" for fn in savlist[i:j+1]] + [".", "EXECUTE."] spss.Submit(cmd) spss.Submit("save outfile = 'd:/temp/merged.sav'.") else: cmd = ["ADD FILES /FILE = 'd:/temp/merged.sav' "] + ["/FILE='" + fn + "'" for fn in savlist[i:j]] + [".", "EXECUTE."]

spss.Submit(cmd) spss.Submit("save outfile = 'd:/temp/merged.sav'.") else: print "No files found to merge" END PROGRAM.

--- On Thu, 8/14/08, Greg Chulsky <chulsky@bu.edu> wrote:

> From: Greg Chulsky <chulsky@bu.edu> > Subject: Appending with WRITE; String variables in quotes > To: SPSSX-L@LISTSERV.UGA.EDU > Date: Thursday, August 14, 2008, 9:27 PM > I have a few questions about something I'm trying to do > and don't know > how. Answers to any of the questions would be appreciated. > If you feel > that I should ask each question in a separate E-mail, > please tell me. > The reason I put them all together was to try to keep this > from clogging > inboxes. > > INTRO: I have written code, using syntax and some Python, > that imports > data from a text file (using GET DATA), manipulates it in > various ways, > and then writes it to a different text file (using WRITE). > When > completed, this will be a batch operation performed on > thousands of files. > > QUESTIONS: > > 1) While thousands of files should be read in, the output > should be a > single file. Whenever I run the code, the WRITE command > overwrites the > file. Is there any way to make it append instead? > > 2) I need these files to be read in an organized fashion, > according to > how they are named. I've tried an experiment by > writing the code this way: > > LOOP #HEMI=rh lh. > > GET DATA /TYPE = TXT > /FILE = 'C:\Documents and Settings\Cog Neuro > Psych > Lab\Desktop\sub009_dw_i'+ > 'n_angular_gyrus_#HEMI.amp' > > [...various operations...] > > WRITE OUTFILE="C:\Documents and Settings\Cog > Neuro Psych > Lab\Desktop\test_#HEMI.txt" > /ALL. > EXECUTE. > > END LOOP. > > This does not work, and so far as I understand, it's > because syntax > won't substitute string variables within quotes. Does > anybody know a > way to do this that would actually work? > > ---- > > If anyone can shed any light on either of these, I will be > deeply grateful. > > -Greg > > ===================== > To manage your subscription to SPSSX-L, send a message to > LISTSERV@LISTSERV.UGA.EDU (not to SPSSX-L), with no body > text except the > command. To leave the list, send the command > SIGNOFF SPSSX-L > For a list of commands to manage subscriptions, send the > command > INFO REFCARD

===================== To manage your subscription to SPSSX-L, send a message to LISTSERV@LISTSERV.UGA.EDU (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD


Back to: Top of message | Previous page | Main SPSSX-L page