473,385 Members | 1,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 3472
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Stu Cazzo | last post by:
I have the following: String myStringArray; String myString = "98 99 100"; I want to split up myString and put it into myStringArray. If I use this: myStringArray = myString.split(" "); it...
11
by: Carlos Ribeiro | last post by:
Hi all, While writing a small program to help other poster at c.l.py, I found a small inconsistency between the handling of keyword parameters of string.split() and the split() method of...
6
by: Senthil | last post by:
Code ---------------------- string Line = "\"A\",\"B\",\"C\",\"D\""; string Line2 = Line.Replace("\",\"","\"\",\"\""); string CSVColumns = Line2.Split("\",\"".ToCharArray());
19
by: David Logan | last post by:
We need an additional function in the String class. We need the ability to suppress empty fields, so that we can more effectively parse. Right now, multiple whitespace characters create multiple...
4
by: Itzik | last post by:
can i split this string string str = "aa a - bb-b - ccc" with this delimiter string del = " - " i want recieve 3 items : "aa a" , "bb-b" , "ccc"
4
by: Crirus | last post by:
There is a function somewhere to split a string with multiple tokens at a time? Say I have this: aaaa#bbbbb*ccccc$dddd I whould like to split it so the result whould be aaaa bbb
14
by: Ron | last post by:
Hello, I am trying to parse a string on the newline char. I guess vbCrLf is a string constant. How can I parse my string - data - on the newline char? .... data += ASCII.GetString(buffer, 0,...
3
by: Ben | last post by:
Hi I am creating a dynamic function to return a two dimensional array from a delimeted string. The delimited string is like: field1...field2...field3... field1...field2...field3......
5
by: kurt sune | last post by:
The code: Dim aLine As String = "cat" & vbNewLine & "dog" & vbNewLine & "fox" & vbNewLine Dim csvColumns1 As String() = aLine.Split(vbNewLine, vbCr, vbLf) Dim csvColumns2 As String() =...
2
by: Digital Fart | last post by:
following code would split a string "a != b" into 2 strings "a" and "b". but is there a way to know what seperator was used? string charSeparators = { "=", ">=", "<=" , "!=" }; string s1 =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.