whitespace within a string

B

Bart Nessux

Is there a function/module that can be used to throw out extra whitespace
that appears within a string? The problem that I have is this: Before any
configuration is done to my files, they have lines with tabs in between the
words like this:

"disable = yes"

After configuring the files using the operating system's administration
tools, the OS rewrites the files to contain spaces instead of tabs. So now,
the file looks like this:

"disable = yes"

I would like my script to work in either situation. Right now, it only works
with spaces, not tabs. Below is the script:

def enable_ssh():
# works on 10.3 systems, not 10.2 systems
import os
os.chdir('/etc/xinetd.d')
find = 'disable = yes'
replace = 'disable = no'
x = file('ssh')
data = x.read()
x.close()
search = str.find(data, find)
if search >= 0:
data = data.replace(find, replace)
outputFile = file('ssh', 'w')
outputFile.write(data)
outputFile.close()
print
print "SSH was disabled... service restarted!!!"
print
else:
print
print "SSH was enabled... no action taken."
print
enable_ssh()
 
J

Jeff Epler

You can use the magic of no-arg split() to do this:
def canonize_whitespace(s):
return " ".join(s.split())
'a b c d e'

A regular expression substituion can do the job too
def canonize_whitespace(s):
return re.sub('\s+', ' ', s)
'a b c d e'

Of course, if 'x=y' is accepted just like 'x = y' and 'x = y', then
neither of these approaches is good enough.

def canonize_config_line(s):
if not '=' in s: return s
a, b = s.split("=", 1)
return "%s = %s" % (a.strip(), b.strip())
>>> [canonize_config_line(s) for s in
... ['x=y', 'x\t= y', ' x = y ', "#z"]]
['x = y', 'x = y', 'x = y', '#z']

Jeff
 
E

Erik Max Francis

Bart said:
Is there a function/module that can be used to throw out extra
whitespace
that appears within a string? The problem that I have is this: Before
any
configuration is done to my files, they have lines with tabs in
between the
words like this:

"disable = yes"

If you realy want to collapse any whitespace at all into a single space
character, how about:

' '.join(S.split())
 
B

Bart Nessux

Jeff said:
You can use the magic of no-arg split() to do this:
def canonize_whitespace(s):
return " ".join(s.split())
'a b c d e'

A regular expression substituion can do the job too
def canonize_whitespace(s):
return re.sub('\s+', ' ', s)
'a b c d e'

Of course, if 'x=y' is accepted just like 'x = y' and 'x = y', then
neither of these approaches is good enough.

def canonize_config_line(s):
if not '=' in s: return s
a, b = s.split("=", 1)
return "%s = %s" % (a.strip(), b.strip())
[canonize_config_line(s) for s in
... ['x=y', 'x\t= y', ' x = y ', "#z"]]
['x = y', 'x = y', 'x = y', '#z']

Jeff

Thanks for the tip. I'll give it a try.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,183
Messages
2,570,968
Members
47,518
Latest member
TobiasAxf

Latest Threads

Top