I would like to use multiple command line arguments in my script. If one or all of them are found, the script will produce an output for them (from excel sheet). Please see the code below. Basically the problem is here:
if sh.cell(i, 1).value == sys.argv[1]:
and I would like to use
if sh.cell(i, 1).value == sys.argv[1:]:
so the operator "==" is wrong. I would like to use
> python Read_xls_files.py group1 group2 group3
and if one or two or all of these are found in the excel file, certain cells are printed (see the code below)
#------------ functions ------------------ def uniq(inlist): # order preserving uniques = [] for item in inlist: if item not in uniques: uniques.append(item) return uniques
> I would like to use multiple command line arguments in my script. If
> one or all of them are found, the script will produce an output for
> them (from excel sheet). Please see the code below. Basically the
> problem is here:
> if sh.cell(i, 1).value == sys.argv[1]:
> and I would like to use
> if sh.cell(i, 1).value == sys.argv[1:]:
> so the operator "==" is wrong. I would like to use
> > python Read_xls_files.py group1 group2 group3
> and if one or two or all of these are found in the excel file, certain
> cells are printed (see the code below)
> #----------- main -----------------------
> #Get the first sheet either by name
> sh = wb.sheet_by_name(u'data')
> hlo = []
> for i in range(len(sh.col_values(8))):
> if sh.cell(i, 1).value == sys.argv[1]:
> hlo.append(sh.cell(i, 8).value)
> sposti = uniq(hlo)
> for i in range(len(sposti)):
> print sposti[i] + ';'
First things first -- the majority of the people you find on this
list, contribute their own time and energy towards helping (we have
jobs, families and mortgages to pay etc..) -- perhaps read that as
"we're not going to do everything for you". (I 'awoke' in a good mood,
maybe I'm tending towards a bad mood as I just pranged my car -- so
forgive me).
You'd be better off with a set for 'in' before appending to the list;
Plenty of 'recipes' that do that.
You realise that no single item will ever == to a list?
>>> 'test' == ['test']
False
No doubt, my un-necessary rant, and John/Chris may respond further,
but you worked out the last problem on your own :) Go for it, and if
you can't, we'll be here.
> Thanks, I'll have to check that. I found also this:
> for grp in sys.argv[1:]: > for i in range(len(sh.col_values(8))): > if sh.cell(i, 1).value == grp: > hlo.append(sh.cell(i, 8).value)
Extremely inefficient.
> 2009/10/30 Chris Withers <ch...@simplistix.co.uk>: >> johannes rara wrote: >>> I would like to use multiple command line arguments in my script. >> ...then I suggest you have a look at argparse:
>>> for i in range(len(sh.col_values(8))): >>> if sh.cell(i, 1).value == sys.argv[1]: >> The above would then likely become:
>> if sh.cell(i, 1).value in options.special_values
Ummmm ... why not if sh.cell(i, 1).value in sys.argv[1:]: ?
Better: at start-up, do: argset = set(sys.argv[1:]) then each iteration do: if sh.cell(i, 1).value in argset:
For alliteration aficionados and fans of Sesame Street, today's letter is "b". The for statement is bordering on being a bit bloated, baroque and byzantine; try: for i in xrange(sh.nrows):
> On 30/10/2009 5:50 PM, johannes rara wrote:
>> Thanks, I'll have to check that. I found also this:
>> for grp in sys.argv[1:]:
>> for i in range(len(sh.col_values(8))):
>> if sh.cell(i, 1).value == grp:
>> hlo.append(sh.cell(i, 8).value)
> Extremely inefficient.
>> 2009/10/30 Chris Withers <ch...@simplistix.co.uk>:
>>> johannes rara wrote:
>>>> I would like to use multiple command line arguments in my script.
>>> ...then I suggest you have a look at argparse:
>>>> for i in range(len(sh.col_values(8))):
>>>> if sh.cell(i, 1).value == sys.argv[1]:
>>> The above would then likely become:
>>> if sh.cell(i, 1).value in options.special_values
> Ummmm ... why not
> if sh.cell(i, 1).value in sys.argv[1:]:
> ?
> Better: at start-up, do:
> argset = set(sys.argv[1:])
> then each iteration do:
> if sh.cell(i, 1).value in argset:
> For alliteration aficionados and fans of Sesame Street, today's letter
> is "b". The for statement is bordering on being a bit bloated, baroque
> and byzantine; try:
> for i in xrange(sh.nrows):
> 2009/10/30 John Machin <sjmac...@lexicon.net>: >> On 30/10/2009 5:50 PM, johannes rara wrote: >>> Thanks, I'll have to check that. I found also this:
>>> for grp in sys.argv[1:]: >>> for i in range(len(sh.col_values(8))): >>> if sh.cell(i, 1).value == grp: >>> hlo.append(sh.cell(i, 8).value)
I thought that the idea is meant to be that the asker selects the best answer except if one of the answerers has rep > 10K ;-) which isn't the case here. Did you know that you can change your mind?