473,322 Members | 1,431 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,322 software developers and data experts.

string.split bug?

string.split("") ==> []
string.split("",",") ==> ['']

I did not expect these to have different outputs.

I have a string with comma delimited numbers.
There can be zero or more numbers in the string
s = "0x41, 0x42"

I wanted to do
numbers = map(lambda x: int(x,16), string.split(s,","))

However, when there are no numbers, this generates an error.
Jul 18 '05 #1
2 2198
MetalOne wrote:
string.split("") ==> []
string.split("",",") ==> ['']

I did not expect these to have different outputs.

I have a string with comma delimited numbers.
There can be zero or more numbers in the string
s = "0x41, 0x42"

I wanted to do
numbers = map(lambda x: int(x,16), string.split(s,","))

However, when there are no numbers, this generates an error.


It's not a bug, it's a feature. The same question was asked on python-dev
recently, and it turned out that str.split() and str.split(separator) are
intended to work differently.

A slightly modernized variant of your example could then be:
def numbers(s, sep=None): .... return [int(x, 16) for x in s.split(sep) if x]
.... numbers("aa bb cc\n") [170, 187, 204] numbers("aa,bb,cc", ",") [170, 187, 204] numbers("", ",")

[]

Peter

Jul 18 '05 #2
MetalOne wrote:
string.split("") ==> []
string.split("",",") ==> ['']

I did not expect these to have different outputs.


S.split(None) is a special case which keys off of all whitespace, not
just a single delimiter string. So when presented with an input string
that contains nothing but whitespace, it strips everything and doesn't
find any tokens at all.

--
__ Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
/ \ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
\__/ Who, my friend, can scale Heaven?
-- _The Epic of Gilgamesh_
Jul 18 '05 #3

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...
20
by: hagai26 | last post by:
I am looking for the best and efficient way to replace the first word in a str, like this: "aa to become" -> "/aa/ to become" I know I can use spilt and than join them but I can also use regular...
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...
3
by: Hetal Shah | last post by:
It is a C# code. I have a string like, one||two||three I want to split it into one, two and three. string str = "one||two||three"; string myStrs = str.Split("||");
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
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 =...
12
by: John | last post by:
Hi I have a multi-line address field which has each line separated by CRLF. How can I split this field into individual strings using crlf as separator? Thanks Regards
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.