471,338 Members | 1,491 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,338 software developers and data experts.

How to split string

I have a string of a variable length and I need to split the string
in strings of 6 characters .
But if the 6th character is not space then I must split the string
at possition before the 6th character.

For example:
if the main string S is

S='abcde fghc ijkl mnop'

the result should be
abcde
fghc
ijkl
mnop
Do you have any idea how it can be done?
Thank you
L.
Dec 5 '07 #1
7 3329
On Dec 5, 10:55 am, Johny <pyt...@hope.czwrote:
I have a string of a variable length and I need to split the string
in strings of 6 characters .
But if the 6th character is not space then I must split the string
at possition before the 6th character.

For example:
if the main string S is

S='abcde fghc ijkl mnop'

the result should be
abcde
fghc
ijkl
mnop

Do you have any idea how it can be done?
Thank you
L.
That's not a great example of what you are looking for, because that
result just looks like:
'\n'.join(S.split())

What result would you be looking for with say:
S='this is just a random sequence of letters courtesy of monkeys on
typewriters.'
?
Dec 5 '07 #2
Johny wrote:
I have a string of a variable length and I need to split the string
in strings of 6 characters .
But if the 6th character is not space then I must split the string
at possition before the 6th character.

For example:
if the main string S is

S='abcde fghc ijkl mnop'

the result should be
abcde
fghc
ijkl
mnop
Do you have any idea how it can be done?
>>print textwrap.fill('abcde fghc ijkl mnop verylongword', width=6)
abcde
fghc
ijkl
mnop v
erylon
gword

Peter
Dec 5 '07 #3
On Dec 5, 11:55 am, Chris <cwi...@gmail.comwrote:
On Dec 5, 10:55 am, Johny <pyt...@hope.czwrote:


I have a string of a variable length and I need to split the string
in strings of 6 characters .
But if the 6th character is not space then I must split the string
at possition before the 6th character.
For example:
if the main string S is
S='abcde fghc ijkl mnop'
the result should be
abcde
fghc
ijkl
mnop
Do you have any idea how it can be done?
Thank you
L.

That's not a great example of what you are looking for, because that
result just looks like:
'\n'.join(S.split())

What result would you be looking for with say:
S='this is just a random sequence of letters courtesy of monkeys on
typewriter.'
Chris,
Thank you for your reply.
So, let's suppose the example:
S='this is just a random sequence of letters courtesy of monkeys on
typewriter.'
and the length of split strings is now 10( instead of 6)
The example should be like this
this is
just a
random
sequence
of letters
courtesy
of monkeys
on
typewriter
In other words, the string should be split at every 10th possition but
if the 10th character is space, then the string must be split at the
nearest space before the 10th possition.

It could be better explained if the length of split strings will be
20.

S='this is just a random sequence of letters courtesy of monkeys on
typewriter.'
Results:

this is just a
random sequence of
letters courtesy of
monkeys on
typewriter.'
Any idea how to do that?
Thank you
L.


Dec 5 '07 #4
On Dec 5, 1:46 pm, Johny <pyt...@hope.czwrote:
On Dec 5, 11:55 am, Chris <cwi...@gmail.comwrote:
On Dec 5, 10:55 am, Johny <pyt...@hope.czwrote:
I have a string of a variable length and I need to split the string
in strings of 6 characters .
But if the 6th character is not space then I must split the string
at possition before the 6th character.
For example:
if the main string S is
S='abcde fghc ijkl mnop'
the result should be
abcde
fghc
ijkl
mnop
Do you have any idea how it can be done?
Thank you
L.
That's not a great example of what you are looking for, because that
result just looks like:
'\n'.join(S.split())
What result would you be looking for with say:
S='this is just a random sequence of letters courtesy of monkeys on
typewriter.'

Chris,
Thank you for your reply.
So, let's suppose the example:
S='this is just a random sequence of letters courtesy of monkeys on
typewriter.'
and the length of split strings is now 10( instead of 6)
The example should be like this
this is
just a
random
sequence
of letters
courtesy
of monkeys
on
typewriter

In other words, the string should be split at every 10th possition but
if the 10th character is space, then the string must be split at the
nearest space before the 10th possition.

It could be better explained if the length of split strings will be
20.

S='this is just a random sequence of letters courtesy of monkeys on
typewriter.'

Results:

this is just a
random sequence of
letters courtesy of
monkeys on
typewriter.'

Any idea how to do that?
Thank you
L.
To be honest I couldn't think of a neat way of doing it.
What seems to look right, albeit fairly ugleh was this.

import string
def digest_chunks(input_string, max_length):
tmp = []
while input_string:
if len(input_string) >= max_length:
if input_string[max_length] not in string.whitespace:
y = max_length - input_string[max_length::-1].find('
')
if not y:
if len(input_string) < max_length:
tmp.append(input_string)
input_string = ''
else:
y = input_string[::-1].find(' ', 1)
tmp.append(input_string[:y])
input_string = input_string[y:]
else:
tmp.append(input_string[:y])
input_string = input_string[y:]
else:
tmp.append(input_string[:max_length])
input_string = input_string[max_length:]
else:
tmp.append(input_string)
input_string = ''

return '\n'.join(tmp)

s = 'this is just a random sequence of letters courtesy of monkeys on
typewriters.'

print digest_chunks(s, 20)
>python -u "test.py"
this is just a
random sequence of
letters courtesy of
monkeys on
typewriters.
>Exit code: 0
Dec 5 '07 #5
In other words, the string should be split at every 10th possition but
if the 10th character is space, then the string must be split at the
nearest space before the 10th possition.

It could be better explained if the length of split strings will be
20.

S='this is just a random sequence of letters courtesy of monkeys on
>typewriter.'

Results:

this is just a
random sequence of
letters courtesy of
monkeys on
typewriter.'

Any idea how to do that?
As mentioned previously, it sounds like you're looking for the
built-in "textwrap" module:
>>s = "this is just a random sequence of letters courtesy of
monkeys on typewriter."
>>import textwrap
print textwrap.fill(s, 20)
this is just a
random sequence of
letters courtesy of
monkeys on
typewriter.

http://docs.python.org/lib/module-textwrap.html

The Python library has already done all the heavy lifting--no
need to re-invent the wheel.

-tkc

Dec 5 '07 #6
On 2007-12-05, Tim Chase <py*********@tim.thechases.comwrote:
http://docs.python.org/lib/module-textwrap.html

The Python library has already done all the heavy lifting--no
need to re-invent the wheel.
Well no, clearly we need xwrap methods and a ctextwrap module. ;)

--
Neil Cerutti
Dec 5 '07 #7
Thank you all who replied particularly Chris and Tim.
L.
Dec 6 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Stu Cazzo | last post: by
11 posts views Thread by Carlos Ribeiro | last post: by
6 posts views Thread by Senthil | last post: by
4 posts views Thread by Itzik | last post: by
4 posts views Thread by Crirus | last post: by
5 posts views Thread by kurt sune | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.