By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,660 Members | 1,102 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,660 IT Pros & Developers. It's quick & easy.

string splitting

P: 56
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
Share this Question
Share on Google+
5 Replies


bartonc
Expert 5K+
P: 6,596
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

P: 56
Thanks, I'll start tinkering with this a bit.
Jun 13 '07 #3

bvdet
Expert Mod 2.5K+
P: 2,851
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
Expert Mod 2.5K+
P: 2,851
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
Expert Mod 2.5K+
P: 2,851
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

Post your reply

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