Re: converting a sed / grep / awk / . . . bash pipe line into python

2008-09-03 Thread bearophileHUGS
Roy Smith: > But, along those lines, I've often thought that split() needed a way to not > just limit the number of splits, but to also throw away the extra stuff. > Getting the first N fields of a string is something I've done often enough > that refactoring the slicing operation right into the sp

Re: converting a sed / grep / awk / . . . bash pipe line into python

2008-09-03 Thread Roy Smith
In article <[EMAIL PROTECTED]>, Peter Otten <[EMAIL PROTECTED]> wrote: > Roy Smith wrote: > > > In article <[EMAIL PROTECTED]>, > > Peter Otten <[EMAIL PROTECTED]> wrote: > > > >> > I might take it one step further, however, and do: > >> > > >> >> fields = line.split()[:2] > >> >>

Re: converting a sed / grep / awk / . . . bash pipe line into python

2008-09-03 Thread Peter Otten
Roy Smith wrote: > In article <[EMAIL PROTECTED]>, > Peter Otten <[EMAIL PROTECTED]> wrote: > >> > I might take it one step further, however, and do: >> > >> >> fields = line.split()[:2] >> >> a, b = map(int, fields) >> > >> > in fact, I might even get rid of the very generic,

Re: converting a sed / grep / awk / . . . bash pipe line into python

2008-09-03 Thread Roy Smith
In article <[EMAIL PROTECTED]>, [EMAIL PROTECTED] wrote: > Roy Smith: > > No reason to limit how many splits get done if you're > > explicitly going to slice the first two. > > You are probably right for this problem, because most lines are 2 > items long, but in scripts that have to process li

Re: converting a sed / grep / awk / . . . bash pipe line into python

2008-09-03 Thread Roy Smith
In article <[EMAIL PROTECTED]>, Peter Otten <[EMAIL PROTECTED]> wrote: > > I might take it one step further, however, and do: > > > >> fields = line.split()[:2] > >> a, b = map(int, fields) > > > > in fact, I might even get rid of the very generic, but conceptually > > overkill,

Re: converting a sed / grep / awk / . . . bash pipe line into python

2008-09-03 Thread bearophileHUGS
Roy Smith: > No reason to limit how many splits get done if you're > explicitly going to slice the first two. You are probably right for this problem, because most lines are 2 items long, but in scripts that have to process lines potentially composed of many parts, setting a max number of parts sp

Re: converting a sed / grep / awk / . . . bash pipe line into python

2008-09-03 Thread Peter Otten
Roy Smith wrote: > In article <[EMAIL PROTECTED]>, > Peter Otten <[EMAIL PROTECTED]> wrote: > >> Without them it looks better: >> >> import sys >> for line in sys.stdin: >> try: >> a, b = map(int, line.split(None, 2)[:2]) >> except ValueError: >> pass >> else: >>

Re: converting a sed / grep / awk / . . . bash pipe line into python

2008-09-03 Thread Roy Smith
In article <[EMAIL PROTECTED]>, Peter Otten <[EMAIL PROTECTED]> wrote: > Without them it looks better: > > import sys > for line in sys.stdin: > try: > a, b = map(int, line.split(None, 2)[:2]) > except ValueError: > pass > else: > if a + b == 42: >

Re: converting a sed / grep / awk / . . . bash pipe line into python

2008-09-03 Thread Peter Otten
hofer wrote: > Something I have to do very often is filtering / transforming line > based file contents and storing the result in an array or a > dictionary. > > Very often the functionallity exists already in form of a shell script > with sed / awk / grep , . . . > and I would like to have the s

Re: converting a sed / grep / awk / . . . bash pipe line into python

2008-09-02 Thread Paul McGuire
On Sep 2, 12:36 pm, hofer <[EMAIL PROTECTED]> wrote: > Hi, > > Something I have to do very often is filtering / transforming line > based file contents and storing the result in an array or a > dictionary. > > Very often the functionallity exists already in form of a shell script > with sed / awk /

Re: converting a sed / grep / awk / . . . bash pipe line into python

2008-09-02 Thread Marc 'BlackJack' Rintsch
On Tue, 02 Sep 2008 10:36:50 -0700, hofer wrote: > sed 's/\.\..*//' \### remove '//' comments | sed 's/#.*//' Comment does not match the code. Or vice versa. :-) Untested: from __future__ import with_statement from itertools import ifilter, ifilterfalse, imap def is_junk(line): line

converting a sed / grep / awk / . . . bash pipe line into python

2008-09-02 Thread hofer
Hi, Something I have to do very often is filtering / transforming line based file contents and storing the result in an array or a dictionary. Very often the functionallity exists already in form of a shell script with sed / awk / grep , . . . and I would like to have the same implementation in m