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.
5 1360
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.
Thanks, I'll start tinkering with this a bit.
bvdet 2,851
Expert Mod 2GB
This is probably not the best way, but it seems to work: - def indexList(s, item, i=0, j=False):
-
i_list = []
-
while True:
-
if j:
-
if i > j: return i_list[:-1]
-
try:
-
i = s.index(item, i)
-
i_list.append(i)
-
i += 1
-
except:
-
return i_list
-
-
def split300(s):
-
if len(s) <= 300: return [s]
-
outList = []
-
while True:
-
try:
-
i = indexList(s,' ',j=300)[-1]
-
outList.append(s[:i])
-
s = s[i+1:]
-
except: return outList
Using recursion: - def split300r(s):
-
if len(s) <= 300: return [s]
-
i = indexList(s,' ',j=300)[-1]
-
return s[:i], split300r(s[i+1:])
bvdet 2,851
Expert Mod 2GB
Using recursion: - def split300r(s):
-
if len(s) <= 300: return [s]
-
i = indexList(s,' ',j=300)[-1]
-
return s[:i], split300r(s[i+1:])
Oops! The recursion function does not work correctly.
bvdet 2,851
Expert Mod 2GB
Oops! The recursion function does not work correctly.
Maybe this will work: - def split300r(s):
-
if len(s) <= 300: return [s]
-
i = indexList(s,' ',j=300)[-1]
-
return [s[:i]] + split300r(s[i+1:])
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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;
...
|
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...
|
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() =...
|
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...
|
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...
|
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:...
|
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 ...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
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,...
| |