- Single Text Replacement with Curly Brackets
- Multiple Text Replacements with Curly Brackets
- Text Replacements with F-Strings
- Multiple Text Replacements with Percent Signs
- Multiple Text Replacements with locals()
Summary
A handy technique in Python are text replacements: we insert tiny pieces of text such as variable names into a larger piece of text such as an SPSS syntax command.
In this tutorial, we'll simply build and print SPSS syntax. Readers who also want to run the Python generated syntax may use health-costs.sav, partly shown below.
Single Text Replacement with Concatenation
Before diving into text replacements, I'd like to point out that you could perform them with a simple string concatenation. Our first example shows how it's done.
begin program python3.
xVars = ['alco','cigs','exer']
for xVar in xVars:
print("GRAPH/SCATTER " + xVar + " WITH COSTS.")
end program.
Note that we specified some SPSS variable names as a Python list holding quoted strings. A nicer option for doing so is expanding the SPSS TO keyword. We'll show how to make this work in Looping over SPSS Commands with Python.
Result
Ok, so our concatenation works. However, it's pretty cumbersome, especially for multiple text replacements. So let's now try something more elegant.
Single Text Replacement with Curly Brackets
For a very basic text replacement, just enter a pair of curly brackets into some Python string. After closing off your string, add .format() with some text replacement between the parentheses.
begin program python3.
xVars = ['alco','cigs','exer']
for xVar in xVars:
print("GRAPH/SCATTER {} WITH COSTS.".format(xVar))
end program.
Note that this code is simpler and more readable than our first example. However, it creates the exact same SPSS syntax. The figure below clarifies its basic structure.
Multiple Text Replacements with Curly Brackets
Let's now add some more flexibility to our SPSS scatterplot syntax: we'll also allow for multiple y-variables and a (sub)title.
Since the required SPSS syntax becomes longer, it's wise (but not strictly necessary) to use multiple lines for it. The example below does just that by enclosing the basic GRAPH command in triple single quotes.
begin program python3.
xVars = ['alco','cigs','exer']
yVar = 'costs'
subTitle = 'All Respondents | N = 525'
for xVar in xVars:
print('''
GRAPH/SCATTER {} WITH {}
/TITLE 'MY SCATTERPLOT'
/SUBTITLE '{}'.'''.format(xVar,yVar,subTitle))
end program.
Note that the curly brackets occur 3 times now. By default, the first pair is replaced by the first element specified after format
and so on.
Result
Rather than simply using empty curly brackets, we can be more specific regarding what they're to be replaced with: in the example below, we'll add indices to them which refer to the elements in the tuple specified by format
. Like so, we can choose in which order these replacements are inserted into our string object.
begin program python3.
xVars = ['alco','cigs','exer']
yVar = 'costs'
subTitle = 'All Respondents | N = 525'
for xVar in xVars:
print('''
GRAPH/SCATTER {2} WITH {1}
/TITLE 'MY SCATTERPLOT'
/SUBTITLE '{0}'.'''.format(subTitle,yVar,xVar))
end program.
Perhaps even better than indices, we can also choose names for our placeholders. These may or may not correspond to Python object that have already been defined.
begin program python3.
xVars = ['alco','cigs','exer']
yVar = 'costs'
subTitle = 'All Respondents | N = 525'
for xVar in xVars:
print('''
GRAPH/SCATTER {x} WITH {y}
/TITLE 'MY SCATTERPLOT'
/SUBTITLE '{subTitle}'.'''.format(subTitle = subTitle,y = yVar,x = xVar))
end program.
Regarding this syntax, note that
- placeholder {x} is replaced by xVar;
- placeholder {y} is replaced by yVar;
- placeholder {subTitle} is replaced by subTitle.
Text Replacements with F-Strings
Python 3.6 (available for SPSS 27 or higher) introduces f-strings: we can use previously defined Python objects as placeholder names with or without any further specification if we precede a string with “f” (for format).
begin program python3.
xVars = ['alco','cigs','exer']
yVar = 'costs'
subTitle = 'All Respondents | N = 525'
for xVar in xVars:
print(f'''
GRAPH/SCATTER {xVar} WITH {yVar}
/TITLE 'MY SCATTERPLOT'
/SUBTITLE '{subTitle}'.''')
end program.
Note that this works quite similarly to using locals()
.
If desired, you can combine this with raw strings which basically disable escaping with backslashes.
begin program python3.
subFolder = 'ruben'
print(rf'd:\data\new file\{subFolder}\here')
end program.
Multiple Text Replacements with Percent Signs
An older method for text replacements in Python is using escape sequences indicated by percent signs:
- use
%s
as a placeholder for strings; - use
%d
as a placeholder for integers; - use
%f
as a placeholder for floats.
After specifying some string, specify your text replacements as a tuple preceded by a percent sign.
begin program python3.
xVars = ['alco','cigs','exer']
yVar = 'costs'
subTitle = 'All Respondents | N = 525'
for xVar in xVars:
print('''
GRAPH/SCATTER %s WITH %s
/TITLE 'MY SCATTERPLOT'
/SUBTITLE '%s'.'''%(xVar,yVar,subTitle))
end program.
Somewhat surprisingly, %s
usually works fine for numbers as well as strings.
Multiple Text Replacements with locals()
As previously discussed, you can directly insert Python objects into strings by using f-strings. An older method that works similary is locals()
. The syntax below shows how it works.
begin program python3.
xVars = ['alco','cigs','exer']
yVar = 'costs'
subTitle = 'All Respondents | N = 525'
for xVar in xVars:
print('''
GRAPH/SCATTER %(xVar)s WITH %(yVar)s
/TITLE 'MY SCATTERPLOT'
/SUBTITLE '%(subTitle)s'.'''%locals())
end program.
Note that %(xVar)s is a placeholder for the (already defined) xVar string object. Adding %locals() after defining some string confirms that all placeholders are to be replaced by local variables: Python objects with the same names that were previously defined.
Before closing off, I should point out that the text replacements discussed in this tutorial are known as Python string formatting. And there's way more to it than I discussed here.
Fortunately, we won't need more than just the basics for getting things done with Python in SPSS.
So I hope you found this helpful.
Thanks for reading!
THIS TUTORIAL HAS 1 COMMENT:
By Andi Hafner on May 16th, 2021
Thanks for providing that succinct explanations, they helped me while investigating the file structure of fail2ban config.