473,385 Members | 1,727 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.

string splitting

first off, sorry for the horrible title.

I can't seem to think of a way to do this, so i was hoping to bounce the idea off of you guys.

I run a irc bot, and sometimes stuff gets cutoff due to strings being too long, so i was wondering if there is a good way to:

if a string X is longer then 300:
-- split it into strings smaller then 300 at the first whitespace after 260

any ideas? im completely stumped. Thanks.
Jun 13 '07 #1
5 1359
bartonc
6,596 Expert 4TB
first off, sorry for the horrible title.

I can't seem to think of a way to do this, so i was hoping to bounce the idea off of you guys.

I run a irc bot, and sometimes stuff gets cutoff due to strings being too long, so i was wondering if there is a good way to:

if a string X is longer then 300:
-- split it into strings smaller then 300 at the first whitespace after 260

any ideas? im completely stumped. Thanks.
Sounds like a job for a regular expression. re objects (from the re module) have this method

split( pattern, string[, maxsplit = 0])

Split string by the occurrences of pattern. If capturing parentheses are used in pattern, then the text of all groups in the pattern are also returned as part of the resulting list. If maxsplit is nonzero, at most maxsplit splits occur, and the remainder of the string is returned as the final element of the list. (Incompatibility note: in the original Python 1.5 release, maxsplit was ignored. This has been fixed in later releases.)

which might do the trick after testing the length of the string and deciding what the value of maxplit is.
Jun 13 '07 #2
Thanks, I'll start tinkering with this a bit.
Jun 13 '07 #3
bvdet
2,851 Expert Mod 2GB
This is probably not the best way, but it seems to work:
Expand|Select|Wrap|Line Numbers
  1. def indexList(s, item, i=0, j=False):
  2.     i_list = []
  3.     while True:
  4.         if j:
  5.             if i > j: return i_list[:-1]
  6.         try:
  7.             i = s.index(item, i)
  8.             i_list.append(i)
  9.             i += 1
  10.         except:
  11.             return i_list
  12.  
  13. def split300(s):
  14.     if len(s) <= 300: return [s]
  15.     outList = []
  16.     while True:
  17.         try: 
  18.             i = indexList(s,' ',j=300)[-1]
  19.             outList.append(s[:i])
  20.             s = s[i+1:]
  21.         except: return outList
Using recursion:
Expand|Select|Wrap|Line Numbers
  1. def split300r(s):
  2.     if len(s) <= 300: return [s]
  3.     i = indexList(s,' ',j=300)[-1]
  4.     return s[:i], split300r(s[i+1:])
Jun 13 '07 #4
bvdet
2,851 Expert Mod 2GB
Using recursion:
Expand|Select|Wrap|Line Numbers
  1. def split300r(s):
  2.     if len(s) <= 300: return [s]
  3.     i = indexList(s,' ',j=300)[-1]
  4.     return s[:i], split300r(s[i+1:])
Oops! The recursion function does not work correctly.
Jun 13 '07 #5
bvdet
2,851 Expert Mod 2GB
Oops! The recursion function does not work correctly.
Maybe this will work:
Expand|Select|Wrap|Line Numbers
  1. def split300r(s):
  2.     if len(s) <= 300: return [s]
  3.     i = indexList(s,' ',j=300)[-1]
  4.     return [s[:i]] + split300r(s[i+1:])
Jun 13 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

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...
3
by: Rico | last post by:
If there are consecutive occurrences of characters from the given delimiter, String.Split() and Regex.Split() produce an empty string as the token that's between such consecutive occurrences. It...
20
by: Guadala Harry | last post by:
In an ASCX, I have a Literal control into which I inject a at runtime. litInjectedContent.Text = dataClass.GetHTMLSnippetFromDB(someID); This works great as long as the contains just...
2
by: npverni | last post by:
I have a string with name value collections (i.e. &foo=1&foo2=2) Can I convert this string to some kind of name/value collection so I can access the values with something like : params = 1; ...
2
by: Trint Smith | last post by:
Ok, My program has been formating .txt files for input into sql server and ran into a problem...the .txt is an export from an accounting package and is only supposed to contain comas (,) between...
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() =...
20
by: Opettaja | last post by:
I am new to c# and I am currently trying to make a program to retrieve Battlefield 2 game stats from the gamespy servers. I have got it so I can retrieve the data but I do not know how to cut up...
4
by: Michele Petrazzo | last post by:
Hello ng, I don't understand why split (string split) doesn't work with the same method if I can't pass values or if I pass a whitespace value: >>> "".split() >>> "".split(" ") But into...
2
by: CharChabil | last post by:
Using Vb.net 2005, I want to read each part in this string in an array (splitting the string) ----------- A1/EXT "BK82 LB73 21233" 105 061018 1804 ----------- That Code that i used is as follow:...
37
by: xyz | last post by:
I have a string 16:23:18.659343 131.188.37.230.22 131.188.37.59.1398 tcp 168 for example lets say for the above string 16:23:18.659343 -- time 131.188.37.230 -- srcaddress 22 ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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.