Appending/Deleting variables to/from variable dictionary

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

Appending/Deleting variables to/from variable dictionary

Jignesh Sutar
Hi
 
Is there a more elegant way to re-write the code below? (Trying to generate a syntax of all string variables (except RespondentID) and variables beginning with "AB" which are then to be dropped form the datafile).
 
    dict1 = spssaux.VariableDict(variableType='string')
    dict2 = spssaux.VariableDict(pattern='^AB')
    vars1 = " ".join(dict1.variables).replace("RespondentID","")
    vars2 = " ".join(dict2.variables)
    dropVars = vars1 + " " + vars2
    #print "\n".join(dropVars)
 
 
Thanks in advance,
Jignesh
Reply | Threaded
Open this post in threaded view
|

Re: Appending/Deleting variables to/from variable dictionary

Jon K Peck
You can judge elegance for yourself, but here is a slightly more direct way to do this.


vars1 = spssaux.VariableDict(variableType='string', pattern="(?!RespondentID$)").variables
vars2 = spssaux.VariableDict(pattern='AB').variables
dropVars= " ".join(vars1 + vars2)

Negative patterns are tricky in a regular expression.  The pattern (?!RespondentID$) matches a string that does not consist entirely of RespondentID.  The pattern expressions in the VariableDict class are automatically anchored at the start of the string, so you don't need to start the expression with ^.

HTH,

Jon Peck (no "h") aka Kim
Senior Software Engineer, IBM
[hidden email]
new phone: 720-342-5621




From:        J Sutar <[hidden email]>
To:        [hidden email],
Date:        12/31/2012 07:39 AM
Subject:        [SPSSX-L] Appending/Deleting variables to/from variable dictionary
Sent by:        "SPSSX(r) Discussion" <[hidden email]>




Hi
 
Is there a more elegant way to re-write the code below? (Trying to generate a syntax of all string variables (except RespondentID) and variables beginning with "AB" which are then to be dropped form the datafile).
 
    dict1 = spssaux.VariableDict(variableType='string')
    dict2 = spssaux.VariableDict(pattern='^AB')
    vars1 = " ".join(dict1.variables).replace("RespondentID","")
    vars2 = " ".join(dict2.variables)
    dropVars = vars1 + " " + vars2

    #print "\n".join(dropVars)
 
 
Thanks in advance,
Jignesh