Re: string split

2009-01-09 Thread Leland
On Jan 9, 12:57 pm, "Jerry Hill"  wrote:
> On Fri, Jan 9, 2009 at 3:39 PM, Benjamin Kaplan
>
> > This looks like a CSV file to me. If that is the case, it is easier to use
> > the built-in csv module than to try to write your own parser.
>
> It should be as easy as this:
>
> import csv
>
> testfile = open('testfile.csv', 'w')
> testdata = """100-01001-001,"Diode,Small Signal,SOT-23",1,D46,
> 100-01004-001,"Diode,High Voltage General
> Purpose,600mA,200V,SOT-23",3,"D24,D72,D104",
> """
> testfile.write(testdata)
> testfile.close()
>
> infile = open('testfile.csv', 'r')
> reader = csv.reader(infile)
> for line in reader:
>     print line
>
> The output of that code is:
>
> ['100-01001-001', 'Diode,Small Signal,SOT-23', '1', 'D46', '']
> ['100-01004-001', 'Diode,High Voltage General
> Purpose,600mA,200V,SOT-23', '3', 'D24,D72,D104', '']
>
> so line[0] is your part number, etc.
>
> --
> Jerry

It works like a charm.

Really appreciate all the helps.
--
http://mail.python.org/mailman/listinfo/python-list


Re: string split

2009-01-09 Thread Jerry Hill
On Fri, Jan 9, 2009 at 3:39 PM, Benjamin Kaplan
> This looks like a CSV file to me. If that is the case, it is easier to use
> the built-in csv module than to try to write your own parser.

It should be as easy as this:

import csv

testfile = open('testfile.csv', 'w')
testdata = """100-01001-001,"Diode,Small Signal,SOT-23",1,D46,
100-01004-001,"Diode,High Voltage General
Purpose,600mA,200V,SOT-23",3,"D24,D72,D104",
"""
testfile.write(testdata)
testfile.close()

infile = open('testfile.csv', 'r')
reader = csv.reader(infile)
for line in reader:
print line


The output of that code is:

['100-01001-001', 'Diode,Small Signal,SOT-23', '1', 'D46', '']
['100-01004-001', 'Diode,High Voltage General
Purpose,600mA,200V,SOT-23', '3', 'D24,D72,D104', '']

so line[0] is your part number, etc.

-- 
Jerry
--
http://mail.python.org/mailman/listinfo/python-list


Re: string split

2009-01-09 Thread Marc 'BlackJack' Rintsch
On Fri, 09 Jan 2009 12:39:22 -0800, Leland wrote:

> It seems work this way, but is there more elegant way to do this?

Yes, the `csv` module in the standard library.

Ciao,
Marc 'BlackJack' Rintsch
--
http://mail.python.org/mailman/listinfo/python-list


Re: string split

2009-01-09 Thread Benjamin Kaplan
On Fri, Jan 9, 2009 at 3:26 PM, Leland  wrote:

> Hi,
>
> I have some formatted strings that I'd like to split and get the
> meaningful data, here is the example of the string format. The big
> difference of these two line are the second double quote set at the
> second line
> 100-01001-001,"Diode,Small Signal,SOT-23",1,D46,
> 100-01004-001,"Diode,High Voltage General Purpose,600mA,200V,SOT-23",
> 3,"D24,D72,D104",
>
> I want to split the string into the following format:
> '100-01001-001', 'Diode,Small Signal,SOT-23', '1', 'D46'
> '100-01004-001', 'Diode,High Voltage General Purpose,600mA,
> 200V,SOT-23', '3', 'D24,D72,D104'
>
[snip code]


This looks like a CSV file to me. If that is the case, it is easier to use
the built-in csv module than to try to write your own parser.


http://docs.python.org/library/csv.html#module-csv
--
http://mail.python.org/mailman/listinfo/python-list


string split

2009-01-09 Thread Leland
Hi,

I have some formatted strings that I'd like to split and get the
meaningful data, here is the example of the string format. The big
difference of these two line are the second double quote set at the
second line
100-01001-001,"Diode,Small Signal,SOT-23",1,D46,
100-01004-001,"Diode,High Voltage General Purpose,600mA,200V,SOT-23",
3,"D24,D72,D104",

I want to split the string into the following format:
'100-01001-001', 'Diode,Small Signal,SOT-23', '1', 'D46'
'100-01004-001', 'Diode,High Voltage General Purpose,600mA,
200V,SOT-23', '3', 'D24,D72,D104'

By doing so, the list then has the meaning of part #, description,
quantity and reference. Here is the way I way to split:
str2=line1.split(',\"', 1) # Split part# and the rest
key = str2[0]
str3 = str2[1]
str4 = str3.split("\",", 1)
Value1 = str4[0]
str5 = str4[1]
str6 = str5.split(",", 1)  # QTY, PARTS & BOM_NOTES
Quanty = str6[0]
str7 = str6[1] # PARTS & BOM_NOTES
if str7[0] == "\"" :
  str8=str7.split("\"", 2)
  str9=str8[1]
else :
  str8=str7.split(",", 1)
  str9=str8[0]
print(key, ":", str9)

It seems work this way, but is there more elegant way to do this?

Thanks,
Leland
--
http://mail.python.org/mailman/listinfo/python-list


string split

2009-01-09 Thread Leland
Hi,

I have some formatted strings that I'd like to split and get the
meaningful data, here is the example of the string format. The big
difference of these two line are the second double quote set at the
second line
100-01001-001,"Diode,Small Signal,SOT-23",1,D46,
100-01004-001,"Diode,High Voltage General Purpose,600mA,200V,SOT-23",
3,"D24,D72,D104",

I want to split the string into the following format:
'100-01001-001', 'Diode,Small Signal,SOT-23', '1', 'D46'
'100-01004-001', 'Diode,High Voltage General Purpose,600mA,
200V,SOT-23', '3', 'D24,D72,D104'

By doing so, the list then has the meaning of part #, description,
quantity and reference. Here is the way I way to split:
str2=line1.split(',\"', 1) # Split part# and the rest
key = str2[0]
str3 = str2[1]
str4 = str3.split("\",", 1)
Value1 = str4[0]
str5 = str4[1]
str6 = str5.split(",", 1)  # QTY, PARTS & BOM_NOTES
Quanty = str6[0]
str7 = str6[1] # PARTS & BOM_NOTES
if str7[0] == "\"" :
  str8=str7.split("\"", 2)
  str9=str8[1]
else :
  str8=str7.split(",", 1)
  str9=str8[0]
print(key, ":", str9)

It seems work this way, but is there more elegant way to do this?

Thanks,
Leland
--
http://mail.python.org/mailman/listinfo/python-list


string split

2009-01-09 Thread Leland
Hi,

I have some formatted strings that I'd like to split and get the
meaningful data, here is the example of the string format. The big
difference of these two line are the second double quote set at the
second line
100-01001-001,"Diode,Small Signal,SOT-23",1,D46,
100-01004-001,"Diode,High Voltage General Purpose,600mA,200V,SOT-23",
3,"D24,D72,D104",

I want to split the string into the following format:
'100-01001-001', 'Diode,Small Signal,SOT-23', '1', 'D46'
'100-01004-001', 'Diode,High Voltage General Purpose,600mA,
200V,SOT-23', '3', 'D24,D72,D104'

By doing so, the list then has the meaning of part #, description,
quantity and reference. Here is the way I way to split:
str2=line1.split(',\"', 1) # Split part# and the rest
key = str2[0]
str3 = str2[1]
str4 = str3.split("\",", 1)
Value1 = str4[0]
str5 = str4[1]
str6 = str5.split(",", 1)  # QTY, PARTS & BOM_NOTES
Quanty = str6[0]
str7 = str6[1] # PARTS & BOM_NOTES

It seems work this way, is there more elegant way to do this?

Thanks,
Leland
--
http://mail.python.org/mailman/listinfo/python-list


Re: String split with " and/or ' and/or \

2008-06-24 Thread Kurt Mueller

Peter Otten schrieb:

Kurt Mueller wrote:

How to (super)split a string (literal) containing " and/or ' and/or
\.
example:

' a  "  b b   "  c\ c '.supersplit(' ')
->
['a', '  b b   ', 'c c']



import shlex
shlex.split(' a  "  b b   "  c\ c ')

['a', '  b b   ', 'c c']


Thanks Peter
Thanks Paul

shlex is what I was looking for.


Grüessli
--
Kurt Müller, [EMAIL PROTECTED]

--
http://mail.python.org/mailman/listinfo/python-list

Re: String split with " and/or ' and/or \

2008-06-24 Thread Peter Otten
Kurt Mueller wrote:

> How to (super)split a string (literal) containing " and/or ' and/or
> \.
> 
> example:
> 
> ' a  "  b b   "  c\ c '.supersplit(' ')
> ->
> ['a', '  b b   ', 'c c']
> 
> 
> Thanks and Grüessli

>>> import shlex
>>> shlex.split(' a  "  b b   "  c\ c ')
['a', '  b b   ', 'c c']

Peter
--
http://mail.python.org/mailman/listinfo/python-list

Re: String split with " and/or ' and/or \

2008-06-24 Thread Paul McGuire
On Jun 24, 3:56 am, Kurt Mueller <[EMAIL PROTECTED]> wrote:
> How to (super)split a string (literal) containing " and/or ' and/or \.
>
> example:
>
> ' a  "  b b   "  c\ c '.supersplit(' ')
> ->
> ['a', '  b b   ', 'c c']
>
> Thanks and Grüessli
> --
> Kurt Müller:
> [EMAIL PROTECTED]

Or did you mean this?

>>> re.split(r'''['"]|\\ ''',' a  "  b b   "  c\ c ')
[' a  ', '  b b   ', '  c', 'c ']

(In your example, you should prefix you quoted string literal with an
r, as in:

r' a  "  b b   "  c\ c '.supersplit(' ')

That way, the '\' character will be treated as just any other
character.

-- Paul

--
http://mail.python.org/mailman/listinfo/python-list


Re: String split with " and/or ' and/or \

2008-06-24 Thread Paul McGuire
On Jun 24, 3:56 am, Kurt Mueller <[EMAIL PROTECTED]> wrote:
> How to (super)split a string (literal) containing " and/or ' and/or \.
>
> example:
>
> ' a  "  b b   "  c\ c '.supersplit(' ')
> ->
> ['a', '  b b   ', 'c c']
>
> Thanks and Grüessli
> --
> Kurt Müller:
> [EMAIL PROTECTED]

>>> re.split(r'''['"\\]''',' a  "  b b   "  c\ c ')
[' a  ', '  b b   ', '  c', ' c ']
--
http://mail.python.org/mailman/listinfo/python-list


String split with " and/or ' and/or \

2008-06-24 Thread Kurt Mueller

How to (super)split a string (literal) containing " and/or ' and/or \.

example:

' a  "  b b   "  c\ c '.supersplit(' ')
->
['a', '  b b   ', 'c c']


Thanks and Grüessli
--
Kurt Müller:
[EMAIL PROTECTED]

--
http://mail.python.org/mailman/listinfo/python-list


Re: string / split method on ASCII code?

2008-03-12 Thread Steven D'Aprano
Sorry for breaking threading by replying to a reply, but I don't seem to 
have the original post.

On Wed, 2008-03-12 at 15:29 -0500, Michael Wieher wrote:
> Hey all,
> 
> I have these annoying textilfes that are delimited by the ASCII char
> for << (only its a single character) and >> (again a single character)
> 
> Their codes are 174 and 175, respectively.
> 
> My datafiles are in the moronic form
> 
> X<>Z

The glyph that looks like "<<" is a left quote in some European countries 
(and a right quote in others, sigh...), and similar for ">>", and are 
usually known as left and right "angle quotation mark", chevron or 
guillemet. And yes, that certainly looks like a moronic form for a data 
file.

But whatever the characters are, we can work with them as normal, if you 
don't mind ignoring that they don't display properly everywhere:

>>> lq = chr(174)
>>> rq = chr(175)
>>> s = "x" + lq + "y" + rq + "z"
>>> print s
x�y�z
>>> s.split(lq)
['x', 'y\xafz']
>>> s.split(rq)
['x\xaey', 'z']


And you can use regular expressions as well. Assuming that the quotes are 
never nested:

>>> import re
>>> r = re.compile(lq + '(.*?)' + rq)
>>> r.search(s).group(1)
'y'


If you want to treat both characters the same:

>>> s = s.replace(lq, rq)
>>> s.split(rq)
['x', 'y', 'z']



-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: string / split method on ASCII code?

2008-03-12 Thread castironpi
>    import re
>    splitter_re = re.compile(chr(174) + '|' + chr(175))
>    for line in file(FILENAME):
>      parts = splitter_re.split(line)
>      do_something(parts)
>
> and then go find a large blunt object with which to bludgeon the
> creator of the file... :)

p>> creator= CreatorOfTheFile()
p>> creator.bludgeon
>
p>>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: string / split method on ASCII code?

2008-03-12 Thread Tim Chase
> I have these annoying textilfes that are delimited by the ASCII char for <<
> (only its a single character) and >> (again a single character)
> 
> Their codes are 174 and 175, respectively.
> 
> My datafiles are in the moronic form
> 
> X<>Z
> 
> I need to split on those freaking characters.  Any tips on how to make split
> work with these things?

If it were a single character, you could just use

   s.split(chr(174))

However, since you need to split on multiple characters, you can use

   import re
   splitter_re = re.compile(chr(174) + '|' + chr(175))
   for line in file(FILENAME):
 parts = splitter_re.split(line)
 do_something(parts)

and then go find a large blunt object with which to bludgeon the 
creator of the file... :)

They aren't exactly ascii characters (0-127), but the above 
should at least do the trick.

-tkc


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: string / split method on ASCII code?

2008-03-12 Thread Carsten Haese
On Wed, 2008-03-12 at 15:29 -0500, Michael Wieher wrote:
> Hey all,
> 
> I have these annoying textilfes that are delimited by the ASCII char
> for << (only its a single character) and >> (again a single character)
> 
> Their codes are 174 and 175, respectively.
> 
> My datafiles are in the moronic form
> 
> X<>Z

Those are decidedly not ASCII codes, since ASCII only goes to 127.

The easiest approach is probably to replace those markers with some
other characters that occurs nowhere else, for example good old NUL, and
then split on that:

line = line.replace(chr(174), "\0")
line = line.replace(chr(175), "\0")
result = line.split("\0")

HTH,

-- 
Carsten Haese
http://informixdb.sourceforge.net


-- 
http://mail.python.org/mailman/listinfo/python-list


string / split method on ASCII code?

2008-03-12 Thread Michael Wieher
Hey all,

I have these annoying textilfes that are delimited by the ASCII char for <<
(only its a single character) and >> (again a single character)

Their codes are 174 and 175, respectively.

My datafiles are in the moronic form

X<>Z

I need to split on those freaking characters.  Any tips on how to make split
work with these things?
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: string split without consumption

2008-02-02 Thread robert
Steve Holden wrote:
> robert wrote:
> [...]
>> but its also wrong regarding partial last lines.
>>
>> re.split obviously doesn't understand \A \Z ^ $ and also \b etc. empty 
>> matches.
>>
> [...]
> Or perhaps you don't understand re?
> 
> It's a tricky thing to start playing with. Look up re.MULTILINE ans 
> re.DOTALL.
> 

I tried "all" variations with (?m) (?s) also, and other things. 
Yet it appeared to me so far, that its not the problem with the 
line modes - none of empty matching works with split.

A basic case:
 >>> re.split(r'\b','ab cwoe fds. fi  foiewj')
['ab cwoe fds. fi  foiewj']

He wants at least "something":

 >>> re.split(r'\b.','ab cwoe fds. fi  foiewj')
['', 'b', '', 'woe', '', 'ds', ' ', 'i', ' ', 'oiewj']


While .findXX and .searchXXX are "all-seeing" :

 >>> re.findall(r'\b','ab cwoe fds. fi  foiewj')
['', '', '', '', '', '', '', '', '', '']



Robert
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: string split without consumption

2008-02-02 Thread Steve Holden
robert wrote:
[...]
> but its also wrong regarding partial last lines.
> 
> re.split obviously doesn't understand \A \Z ^ $ and also \b etc. 
> empty matches.
> 
[...]
Or perhaps you don't understand re?

It's a tricky thing to start playing with. Look up re.MULTILINE ans 
re.DOTALL.

regards
  Steve
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC  http://www.holdenweb.com/

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: string split without consumption

2008-02-02 Thread robert
Jeffrey Froman wrote:
> robert wrote:
> 
>> thanks. Yet this does not work "naturally" consistent in my line
>> processing algorithm - the further buffering. Compare e.g.
>> ss.split('\n')  ..
>>
> 'owi\nweoifj\nfheu\n'.split('\n')
>> ['owi', 'weoifj', 'fheu', '']
> 'owi\nweoifj\nfheu\nxx'.split('\n')
>> ['owi', 'weoifj', 'fheu', 'xx']
> 
> 
> Maybe this works for you?
> 
 re.split(r'(\n)', ss)
> ['owi', '\n', 'weoifj', '\n', 'fheu', '\n', '']
> 

Thanks, thats it


Robert
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: string split without consumption

2008-02-02 Thread robert
Tim Chase wrote:
 this didn't work elegantly as expected:

  >>> ss
 'owi\nweoifj\nfheu\n'
  >>> re.split(r'(?m)$',ss)
 ['owi\nweoifj\nfheu\n']
>>> Do you have a need to use a regexp?
>> I'd like the general case - split without consumption.
> 
> I'm not sure there's a one-pass regex solution to the problem
> using Python's regex engine.  If pre-processing was allowed, one
> could do it.
> 

I only found it partly with inverse logic - findall:

 >>> re.findall(r'(?s).*?(?:\n|$)','owi\nweoifj\nfheu\nxx')
['owi\n', 'weoifj\n', 'fheu\n', 'xx', '']
 >>> re.findall(r'(?s).*?(?:\n|$)','owi\nweoifj\nfheu\n')
['owi\n', 'weoifj\n', 'fheu\n', '']
 >>>

but its also wrong regarding partial last lines.

re.split obviously doesn't understand \A \Z ^ $ and also \b etc. 
empty matches.

 >>> re.split(r'\b(?=\n)','owi\nweoifj\nfheu\n\nxx')
['owi\nweoifj\nfheu\n\nxx']


>> ss.splitlines(True)
>>> ['owi\n', 'weoifj\n', 'fheu\n']
>>>
>> thanks. Yet this does not work "naturally" consistent in my line 
>> processing algorithm - the further buffering. Compare e.g. 
>> ss.split('\n')  ..
> 
> well, one can do
> 
>   >>> [line + '\n' for line in ss.splitlines()]
>   ['owi\n', 'eoifj\n', 'heu\n']
>   >>> [line + '\n' for line in (ss+'xxx').splitlines()]
>   ['owi\n', 'eoifj\n', 'heu\n', 'xxx\n']
> 
> as another try for your edge case.  It's understandable and
> natural-looking
> 

nice for some display purposes, but "wrong" regarding a general 
logic. The 'xxx' is not a complete line in the general case. Its 
and (open) part and should appear so.


Robert

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: string split without consumption

2008-02-02 Thread Jeffrey Froman
robert wrote:

> thanks. Yet this does not work "naturally" consistent in my line
> processing algorithm - the further buffering. Compare e.g.
> ss.split('\n')  ..
> 
> >>> 'owi\nweoifj\nfheu\n'.split('\n')
> ['owi', 'weoifj', 'fheu', '']
> >>> 'owi\nweoifj\nfheu\nxx'.split('\n')
> ['owi', 'weoifj', 'fheu', 'xx']


Maybe this works for you?

>>> re.split(r'(\n)', ss)
['owi', '\n', 'weoifj', '\n', 'fheu', '\n', '']


Jeffrey
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: string split without consumption

2008-02-02 Thread Tim Chase
>>> this didn't work elegantly as expected:
>>>
>>>  >>> ss
>>> 'owi\nweoifj\nfheu\n'
>>>  >>> re.split(r'(?m)$',ss)
>>> ['owi\nweoifj\nfheu\n']
>> Do you have a need to use a regexp?
> 
> I'd like the general case - split without consumption.

I'm not sure there's a one-pass regex solution to the problem
using Python's regex engine.  If pre-processing was allowed, one
could do it.

> ss.splitlines(True)
>> ['owi\n', 'weoifj\n', 'fheu\n']
>>
> 
> thanks. Yet this does not work "naturally" consistent in my line 
> processing algorithm - the further buffering. Compare e.g. 
> ss.split('\n')  ..

well, one can do

  >>> [line + '\n' for line in ss.splitlines()]
  ['owi\n', 'eoifj\n', 'heu\n']
  >>> [line + '\n' for line in (ss+'xxx').splitlines()]
  ['owi\n', 'eoifj\n', 'heu\n', 'xxx\n']

as another try for your edge case.  It's understandable and
natural-looking

-tkc




-- 
http://mail.python.org/mailman/listinfo/python-list


Re: string split without consumption

2008-02-02 Thread robert
Tim Chase wrote:
>> this didn't work elegantly as expected:
>>
>>  >>> ss
>> 'owi\nweoifj\nfheu\n'
>>  >>> re.split(r'(?m)$',ss)
>> ['owi\nweoifj\nfheu\n']
> 
> Do you have a need to use a regexp?

I'd like the general case - split without consumption.

> 
 ss.splitlines(True)
> ['owi\n', 'weoifj\n', 'fheu\n']
> 

thanks. Yet this does not work "naturally" consistent in my line 
processing algorithm - the further buffering. Compare e.g. 
ss.split('\n')  ..

 >>> 'owi\nweoifj\nfheu\n'.split('\n')
['owi', 'weoifj', 'fheu', '']
 >>> 'owi\nweoifj\nfheu\nxx'.split('\n')
['owi', 'weoifj', 'fheu', 'xx']

is consistent in that regard: there is always a last empty or half 
line, which can be fed readily as start to the further input 
buffering.
With the .splitlines(True/False) results you need to fiddle, test 
the last result's last char... Or you fail altogether with False.
So I'd call this a "wrong" implementation.


Robert
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: string split without consumption

2008-02-02 Thread Tim Chase
> this didn't work elegantly as expected:
> 
>  >>> ss
> 'owi\nweoifj\nfheu\n'
>  >>> re.split(r'(?m)$',ss)
> ['owi\nweoifj\nfheu\n']

Do you have a need to use a regexp?

>>> ss.splitlines(True)
['owi\n', 'weoifj\n', 'fheu\n']

-tkc




-- 
http://mail.python.org/mailman/listinfo/python-list


string split without consumption

2008-02-02 Thread robert
this didn't work elegantly as expected:

 >>> ss
'owi\nweoifj\nfheu\n'
 >>> re.split(r'\A',ss)
['owi\nweoifj\nfheu\n']
 >>> re.split(r'\Z',ss)
['owi\nweoifj\nfheu\n']
 >>> re.split(r'$',ss)
['owi\nweoifj\nfheu\n']
 >>> re.split(r'(?s)$',ss)
['owi\nweoifj\nfheu\n']
 >>> re.split(r'(?m)(?s)$',ss)
['owi\nweoifj\nfheu\n']
 >>> re.split(r'(?m)$',ss)
['owi\nweoifj\nfheu\n']
 >>> re.split(r'(?m)\Z',ss)
['owi\nweoifj\nfheu\n']
 >>> re.split(r'(?m)\A',ss)
['owi\nweoifj\nfheu\n']
 >>> re.split(r'(?s)\A',ss)
['owi\nweoifj\nfheu\n']
 >>> re.split(r'(?s)(?m)\A',ss)
['owi\nweoifj\nfheu\n']
 >>>


how to do?


Robert
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: String split

2006-03-28 Thread Peter Otten
Michele Petrazzo wrote:

> Just a question about that "different algorithm", because it force the
> developer to do other work for make the "split" result more "logically
> compatible":
> 
> S = "" # this can become from an external source like config parser
> 
> for s n S.split():
> do the work... # don't go inside
> 
> that isn't "compatible", so python split it into two different methods
> the string, with:
> 
> for s n S.split(','):
> do the work... # run one time into this

No question.

>>> def split_any(s, seps):
... for sep in seps:
... parts = s.split(sep)
... if len(parts) > 1:
... return parts
... raise ValueError
...
>>> split_any(" alpha beta ", [",", None])
['alpha', 'beta']
>>> split_any("alpha,beta", [",", None])
['alpha', 'beta']
>>> split_any("alpha_beta", [",", None])
Traceback (most recent call last):
  File "", line 1, in ?
  File "", line 6, in split_any
ValueError

The answer :-)

Seriously, I think you have to be a bit more explicit on what you want to
know.

Peter

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: String split

2006-03-28 Thread Michele Petrazzo
Peter Otten wrote:
> The documentation for Python 2.4 has a better explanation.
> 

<-cut->

> Quoted after http://docs.python.org/lib/string-methods.html#l2h-202.
> 
> Peter

Thanks, I haven't see it.
Just a question about that "different algorithm", because it force the
developer to do other work for make the "split" result more "logically
compatible":

S = "" # this can become from an external source like config parser

for s n S.split():
   do the work... # don't go inside

that isn't "compatible", so python split it into two different methods
the string, with:

for s n S.split(','):
   do the work... # run one time into this


Michele
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: String split

2006-03-28 Thread Fredrik Lundh
Michele Petrazzo wrote:

> 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 the doc I see:
> """ If sep is not specified or is None, any whitespace string is a
> separator.
> """
>
> In this two cases, split would to return the _same_ result?

split(None) strips the string before splitting it (on runs of whitespace,
not on individual whitespace characters).  see the library reference for
a complete description:

http://docs.python.org/lib/string-methods.html





-- 
http://mail.python.org/mailman/listinfo/python-list


Re: String split

2006-03-28 Thread Peter Otten
Michele Petrazzo wrote:

> 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 the doc I see:
> """ If sep is not specified or is None, any whitespace string is a
> separator.
> """

The documentation for Python 2.4 has a better explanation.

"""
split([sep [,maxsplit]])

Return a list of the words in the string, using sep as the delimiter string.
If maxsplit is given, at most maxsplit splits are done. (thus, the list
will have at most maxsplit+1 elements). If maxsplit is not specified, then
there is no limit on the number of splits (all possible splits are made).
Consecutive delimiters are not grouped together and are deemed to delimit
empty strings (for example, "'1,,2'.split(',')"returns "['1', '', '2']").
The sep argument may consist of multiple characters (for example, "'1, 2,
3'.split(', ')" returns "['1', '2', '3']"). Splitting an empty string with
a specified separator returns "['']". 

If sep is not specified or is None, a different splitting algorithm is
applied. First, whitespace characters (spaces, tabs, newlines, returns, and
formfeeds) are stripped from both ends. Then, words are separated by
arbitrary length strings of whitespace characters. Consecutive whitespace
delimiters are treated as a single delimiter ("'1 2 3'.split()" returns
"['1', '2', '3']"). Splitting an empty string or a string consisting of
just whitespace returns an empty list. 
"""
Quoted after http://docs.python.org/lib/string-methods.html#l2h-202.

Peter
-- 
http://mail.python.org/mailman/listinfo/python-list


String split

2006-03-28 Thread Michele Petrazzo
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 the doc I see:
""" If sep is not specified or is None, any whitespace string is a
separator.
"""

In this two cases, split would to return the _same_ result?

Thanks,
Michele
-- 
http://mail.python.org/mailman/listinfo/python-list