do replacement evenly

O

oyster

I have some strings, and I want to write them into a text files, one
string one line
but there is a requirement: every line has a max length of a certain
number(for example, 10), so I have to replace extra SPACE*3 with
SPACE*2, at the same time, I want to make the string looks good, so,
for "I am123456line123456three"(to show the SPACE clearly, I type it
with a number), the first time, I replace the first SPACE, and get "I
am23456line123456three", then I must replace at the second SPACE
block, so I get "I am23456line23456three", and so on, if no SPACE*3
is found, I have to aString.replace(SPACE*2, SPACE).
I hope I have stated my case clear.

Then the question is, is there a nice solution?

thanx
 
I

Iain King

I have some strings, and I want to write them into a text files, one
string one line
but there is a requirement: every line has a max length of a certain
number(for example, 10), so I have to replace extra SPACE*3 with
SPACE*2, at the same time, I want to make the string looks good, so,
for "I am123456line123456three"(to show the SPACE clearly, I type it
with a number), the first time, I replace the first SPACE, and get "I
am23456line123456three", then I must replace at the second SPACE
block, so I get  "I am23456line23456three", and so on, if no SPACE*3
is found, I have to aString.replace(SPACE*2, SPACE).
I hope I have stated my case clear.

Then the question is, is there a nice solution?

thanx

Assuming you want to crush all spaces into single space, you can:

while " " in s:
s = s.replace(" ", " ")

readable but not efficient. Better:

s = " ".join((x for x in s.split(" ") if x))

Note that this will strip leading and trailing spaces.

Or you can use regexps:

import re
s = re.sub(" {2,}", " ", s)


Iain
 
M

Mike Kazantsev

I have some strings, and I want to write them into a text files, one
string one line
but there is a requirement: every line has a max length of a certain
number(for example, 10), so I have to replace extra SPACE*3 with
SPACE*2, at the same time, I want to make the string looks good, so,
for "I am123456line123456three"(to show the SPACE clearly, I type it
with a number), the first time, I replace the first SPACE, and get "I
am23456line123456three", then I must replace at the second SPACE
block, so I get "I am23456line23456three", and so on, if no SPACE*3
is found, I have to aString.replace(SPACE*2, SPACE).
I hope I have stated my case clear.

Then the question is, is there a nice solution?

Not so nice, but it should be faster than whole lot of string
manipulations, especially on longer lines:

len_line = 55
line = 'Thats a whole line of some utter nonsense ;)'

words = line.split()
count_space = len_line - len(''.join(words))
count_span = len(words) - 1
span_min = (count_space // count_span) * ' '
count_span_max = count_space - (count_span * len(span_min))

line = buffer(words[0])
for word in words[1:]:
if count_span_max:
count_span_max -= 1
line += span_min + ' '
else: line += span_min
line += word

print '%d chars: %r'%(len(line), line)

--
Mike Kazantsev // fraggod.net

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.11 (GNU/Linux)

iEYEARECAAYFAkolHlwACgkQASbOZpzyXnHvmACdHBuV5+fxj3PDrXq65RUAUC9K
T74AnjA2guyCsNfSReBY/QoF5+bcwm1K
=f9Tq
-----END PGP SIGNATURE-----
 
P

python

Here's how we normalize whitespace in multiline blocks of text. Perhaps
you can adapt this routine to your needs? Watch for line wrap.

# clean up duplicate whitespace, leading/trailing whitespace, triple
CRLF's
def fixwhitespace( text ):
output = []
lastLine = ''

# split text into list of individual lines
lines = text.strip().splitlines()
for line in lines:
# remove leading, trailing, and duplicate whitespace within a line
line = ' '.join( line.split( None ) )

# ignore multiple blank lines
if not line and not lastLine:
pass
else:
output.append( line )
lastLine = line

return '\n'.join( output )

Regards,
Malcolm
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top