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

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,"

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,

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

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']

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 m

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

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 sin

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(

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

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

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.MULTI

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 Ste

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'.

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-p

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'

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

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', 'weoi

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

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 wor

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

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

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