Curly brace plague?

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

Curly brace plague?

tko
Hello,

I work with survey data files with raw variable labels that are filled with unecessary programming characters/strings.  Here's what removes almost everything I don't want, except for "{#Company1}", probably because of the curly braces issue.  Any advice on how to escape a curly brace within a string?

Thanks,
tko

begin program.
import spss, spssaux, cgi
vdict=spssaux.VariableDict()
nvars = len(vdict)

html_escape_table = {
    "&": "&",
    ">": ">",
    "<": "<",
    "/": "&fs;",
    }

def html_escape(text):
    """Produce entities within text."""
    return "".join(html_escape_table.get(c,c) for c in text)

for i in range(nvars):
    myvarname = vdict[i].VariableName
    myvarlabel = html_escape(vdict[i].VariableLabel)
    myvarlabel = myvarlabel.replace(" :","")
    myvarlabel = myvarlabel.replace("<b>","")
    myvarlabel = myvarlabel.replace("<&fs;b>","")
    myvarlabel = myvarlabel.replace("{#Company1}", "[Company]")
    myvarlabel = myvarlabel.strip(" '")
    spss.Submit(r"""
        variable labels  %s %s .
                        """ %(myvarname, myvarlabel))
end program.
Reply | Threaded
Open this post in threaded view
|

Re: Curly brace plague?

John F Hall
For what it's worth, have you tried exporting to Word, setting Word to
change curly to straight and then copying back?


Email:    [hidden email]
Website: www.surveyresearch.weebly.com
Skype:    surveyresearcher1
Phone:    (+33) (0) 2.33.45.91.47




-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of tko
Sent: 10 February 2012 09:11
To: [hidden email]
Subject: Curly brace plague?

Hello,

I work with survey data files with raw variable labels that are filled with
unecessary programming characters/strings.  Here's what removes almost
everything I don't want, except for "*{#Company1}*", probably because of the
curly braces issue.  Any advice on how to escape a curly brace within a
string?

Thanks,
tko

begin program.
import spss, spssaux, cgi
vdict=spssaux.VariableDict()
nvars = len(vdict)

html_escape_table = {
    "&": "&amp;",
    ">": "&gt;",
    "<": "&lt;",
    "/": "&fs;",
    }

def html_escape(text):
    """Produce entities within text."""
    return "".join(html_escape_table.get(c,c) for c in text)

for i in range(nvars):
    myvarname = vdict[i].VariableName
    myvarlabel = html_escape(vdict[i].VariableLabel)
    myvarlabel = myvarlabel.replace(" :","")
    myvarlabel = myvarlabel.replace("&lt;b&gt;","")
    myvarlabel = myvarlabel.replace("&lt;&fs;b&gt;","")
    myvarlabel = myvarlabel.replace("*{#Company1}*", "[Company]")
    myvarlabel = myvarlabel.strip(" '")
    spss.Submit(r"""
        variable labels  %s %s .
                        """ %(myvarname, myvarlabel))
end program.


--
View this message in context:
http://spssx-discussion.1045642.n5.nabble.com/Curly-brace-plague-tp5471796p5
471796.html
Sent from the SPSSX Discussion mailing list archive at Nabble.com.

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (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
[hidden email] (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
Reply | Threaded
Open this post in threaded view
|

Re: Curly brace plague?

Jon K Peck
In reply to this post by tko
You are using the string method replace.  In that method, characters are just plain characters, so { is a character like any other, but it looks like your intention is to do something more like a regular expression.  Or are you literally looking for occurrences of "*{#Company1}*"?

For example,
s = 'abc{def'
s.replace("{", "-")

produces
'abc-def'

Using a regular expression replacement might be better, depending on exactly what your intention is.



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




From:        tko <[hidden email]>
To:        [hidden email]
Date:        02/10/2012 01:15 AM
Subject:        [SPSSX-L] Curly brace plague?
Sent by:        "SPSSX(r) Discussion" <[hidden email]>




Hello,

I work with survey data files with raw variable labels that are filled with
unecessary programming characters/strings.  Here's what removes almost
everything I don't want, except for "*{#Company1}*", probably because of the
curly braces issue.  Any advice on how to escape a curly brace within a
string?

Thanks,
tko

begin program.
import spss, spssaux, cgi
vdict=spssaux.VariableDict()
nvars = len(vdict)

html_escape_table = {
   "&": "&amp;",
   ">": "&gt;",
   "<": "&lt;",
   "/": "&fs;",
   }

def html_escape(text):
   """Produce entities within text."""
   return "".join(html_escape_table.get(c,c) for c in text)

for i in range(nvars):
   myvarname = vdict[i].VariableName
   myvarlabel = html_escape(vdict[i].VariableLabel)
   myvarlabel = myvarlabel.replace(" :","")
   myvarlabel = myvarlabel.replace("&lt;b&gt;","")
   myvarlabel = myvarlabel.replace("&lt;&fs;b&gt;","")
   myvarlabel = myvarlabel.replace("*{#Company1}*", "[Company]")
   myvarlabel = myvarlabel.strip(" '")
   spss.Submit(r"""
       variable labels  %s %s .
                       """ %(myvarname, myvarlabel))
end program.


--
View this message in context:
http://spssx-discussion.1045642.n5.nabble.com/Curly-brace-plague-tp5471796p5471796.html
Sent from the SPSSX Discussion mailing list archive at Nabble.com.

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (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


tko
Reply | Threaded
Open this post in threaded view
|

Re: Curly brace plague?

tko
Switched to regular expressions, and it works!  Now the issue is the forward slash (commented out below), which doesn't seem to work with spss.Submit

begin program.
import spss, spssaux, re, cgi
vdict=spssaux.VariableDict()
nvars = len(vdict)


html_escape_table = {
    ">": ">",
    "<": "<",
    "/": "&fs;",
    "&": "&",
    }

def html_escape(text):
    """Produce entities within text."""
    return "".join(html_escape_table.get(c,c) for c in text)

for i in range(nvars):
    myvarname = vdict[i].VariableName
    myvarlabel = html_escape(vdict[i].VariableLabel)
    myvarlabel = re.sub(' :','',myvarlabel)
    myvarlabel = re.sub(r'<b>'+'(?i)','',myvarlabel)
    myvarlabel = re.sub(r'<&fs;b>'+'(?i)','',myvarlabel)
    myvarlabel = re.sub('{#','[',myvarlabel)
    myvarlabel = re.sub(r'\d}',']',myvarlabel)
    #myvarlabel = re.sub('&fs;','/',myvarlabel)
    myvarlabel = myvarlabel.strip(" '")
    myvarlabel = myvarlabel.replace("\n","")
    spss.Submit(r"""
        variable labels  %s %s .
                        """ %(myvarname, myvarlabel))
end program.
Reply | Threaded
Open this post in threaded view
|

Re: Curly brace plague?

Jon K Peck
It looks like you are not quoting the label, so the / will be parsed as introducing a new subcommand.  And if you want to be paranoid and protect against labels where the text includes a quote, you should logic such what you can see in spssaux._smartquote

HTH,

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




From:        tko <[hidden email]>
To:        [hidden email]
Date:        02/10/2012 10:30 AM
Subject:        Re: [SPSSX-L] Curly brace plague?
Sent by:        "SPSSX(r) Discussion" <[hidden email]>




Switched to regular expressions, and it works!  Now the issue is the forward
slash (commented out below), which doesn't seem to work with spss.Submit

begin program.
import spss, spssaux, re, cgi
vdict=spssaux.VariableDict()
nvars = len(vdict)


html_escape_table = {
   ">": "&gt;",
   "<": "&lt;",
   "/": "&fs;",
   "&": "&amp;",
   }

def html_escape(text):
   """Produce entities within text."""
   return "".join(html_escape_table.get(c,c) for c in text)

for i in range(nvars):
   myvarname = vdict[i].VariableName
   myvarlabel = html_escape(vdict[i].VariableLabel)
   myvarlabel = re.sub(' :','',myvarlabel)
   myvarlabel = re.sub(r'&lt;b&gt;'+'(?i)','',myvarlabel)
   myvarlabel = re.sub(r'&lt;&fs;b&gt;'+'(?i)','',myvarlabel)
   myvarlabel = re.sub('{#','[',myvarlabel)
   myvarlabel = re.sub(r'\d}',']',myvarlabel)
   #myvarlabel = re.sub('&fs;','/',myvarlabel)
   myvarlabel = myvarlabel.strip(" '")
   myvarlabel = myvarlabel.replace("\n","")
   spss.Submit(r"""
       variable labels  %s %s .
                       """ %(myvarname, myvarlabel))
end program.


--
View this message in context:
http://spssx-discussion.1045642.n5.nabble.com/Curly-brace-plague-tp5471796p5473262.html
Sent from the SPSSX Discussion mailing list archive at Nabble.com.

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (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