splitting using '\' as a delimiter 
Author Message
 splitting using '\' as a delimiter

I have strings like this:

string = 'value\key'

that I would like to split.  If I try splitting the string like this:

alias = string.split('\\',string)

I get:

alias = ['\']    

How can I do this?

Thanks



Fri, 21 May 2004 07:41:45 GMT  
 splitting using '\' as a delimiter
[posted and mailed]



Quote:
> I have strings like this:
> string = 'value\key'
> that I would like to split.  If I try splitting the string like this:
> alias = string.split('\\',string)
> I get:
> alias = ['\']    

change the order of arguments:
Quote:
>>> string.split('value\\key','\\')

['value', 'key']

or:

Quote:
>>> 'value\\key'.split("\\")

['value', 'key']

sidenote: don't name your variable "string", that would hide the module
with the same name.

chris

Quote:
> How can I do this?

> Thanks

--



Fri, 21 May 2004 08:21:32 GMT  
 splitting using '\' as a delimiter

Quote:
> I have strings like this:

> string = 'value\key'

> that I would like to split.  If I try splitting the string like this:

> alias = string.split('\\',string)

> I get:

> alias = ['\']

> How can I do this?

> Thanks

for me, this works:

import string
buffer = r'value\key'
print string.split(buffer,"\\")

used python 2.1.1 on linux

felix

--
Hi! I'm a .signature virus! copy me into your .signature file to help me
spread!



Fri, 21 May 2004 08:03:28 GMT  
 splitting using '\' as a delimiter

So the string.split and the re.split swap the order of the arguments??!!
What was the logic behind this decision?

Quote:
> > I have strings like this:
> > string = 'value\key'
> > that I would like to split.  If I try splitting the string like this:
> > alias = string.split('\\',string)
> > I get:
> > alias = ['\']

> change the order of arguments:
> >>> string.split('value\\key','\\')
> ['value', 'key']

> or:

> >>> 'value\\key'.split("\\")
> ['value', 'key']



Fri, 21 May 2004 08:30:04 GMT  
 splitting using '\' as a delimiter

Quote:

> So the string.split and the re.split swap the order of the arguments??!!
> What was the logic behind this decision?

I think it was just that, for string.split, the string that you're
splitting is the primary object, so it comes first.
i.e. string.split(x, y) = x.split(y).  Meanwhile, for re.split, the
primary object is the re, so re.split(y, x) = y.split(x).

Well, to be correct, re.split(y, x) = re.compile(y).split(x).

-J



Fri, 21 May 2004 14:23:20 GMT  
 splitting using '\' as a delimiter

Quote:

>So the string.split and the re.split swap the order of the arguments??!!
>What was the logic behind this decision?

Accident, I suspect.  The string is first in string.split because the
delimiter string is optional.  If the delimiter were first, it would be
required.

You could also argue that the two have different focus.  In string.split,
the focus is on the string.  The string is being told to split itself,
based on the delimiters.  In re.split, the focus is on the regular
expression.  The re is being told to take a specific action, in this case
to split up a string.

This little inconsistency remains even with the new syntax:

   string_to_be_split.split( delimiters )
   regular_expression.split( string_to_be_split )
--

  Providenza & Boekelheide, Inc.



Fri, 21 May 2004 14:56:44 GMT  
 splitting using '\' as a delimiter

Quote:
> [snip]
> This little inconsistency remains even with the new syntax:

>    string_to_be_split.split( delimiters )
>    regular_expression.split( string_to_be_split )

But at least this is much clearer.

Alan Winston
Seattle



Sat, 22 May 2004 01:43:36 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. delimiter '::' with split

2. Splitting a string using '/n' as the delimiter

3. 'split' creates extra output

4. Splitting 'and' conditions into multiple conditions

5. Behavior of 'split' command

6. newbie: emacs 'split-string'

7. Splitting a string every 'n'

8. changing read's string delimiter

9. split that includes delimiter ?

10. split on string that included delimiter

11. Splitting on a regex w/o consuming delimiter

12. .scan() using '/' as the regular expression delimiter

 

 
Powered by phpBB® Forum Software