Web Images Videos Maps News Groups Gmail more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Newbie: How to check for multiple command line arguments
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  7 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
johannes rara  
View profile  
 More options Oct 29, 1:34 am
From: johannes rara <johannesr...@gmail.com>
Date: Wed, 28 Oct 2009 16:34:54 +0200
Local: Thurs, Oct 29 2009 1:34 am
Subject: Newbie: How to check for multiple command line arguments
Hi,

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)

Many thanks.
_johannes_

######################## python code #####################
#!/Python26/
# -*- coding: utf-8 -*-

import sys
import xlrd

#------------ functions ------------------
def uniq(inlist):
    # order preserving
    uniques = []
    for item in inlist:
        if item not in uniques:
            uniques.append(item)
    return uniques

#----------- commandlinearguments ------
if len(sys.argv) < 2:
        print 'Usage: python Read_xls_files.py <groupst>'
        sys.exit()

#----------- input ----------------
wb = xlrd.open_workbook('testiexcel.xls')

#----------- 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] + ';'


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jon Clements  
View profile  
 More options Oct 29, 2:10 am
From: Jon Clements <jon...@googlemail.com>
Date: Wed, 28 Oct 2009 08:10:10 -0700 (PDT)
Local: Thurs, Oct 29 2009 2:10 am
Subject: Re: Newbie: How to check for multiple command line arguments

On 28 Oct, 14:34, johannes rara <johannesr...@gmail.com> wrote:

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.

Cheers,

Jon.


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chris Withers  
View profile  
 More options Oct 30, 5:13 pm
From: Chris Withers <ch...@simplistix.co.uk>
Date: Fri, 30 Oct 2009 06:13:25 +0000
Local: Fri, Oct 30 2009 5:13 pm
Subject: Re: [pyxl] Newbie: How to check for multiple command line arguments

johannes rara wrote:
> I would like to use multiple command line arguments in my script.

...then I suggest you have a look at argparse:

http://code.google.com/p/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

cheers,

Chris

--
Simplistix - Content Management, Batch Processing & Python Consulting
            - http://www.simplistix.co.uk


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
johannes rara  
View profile  
 More options Oct 30, 5:50 pm
From: johannes rara <johannesr...@gmail.com>
Date: Fri, 30 Oct 2009 08:50:35 +0200
Local: Fri, Oct 30 2009 5:50 pm
Subject: Re: [pyxl] Re: Newbie: How to check for multiple command line arguments
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)

-Johannes

2009/10/30 Chris Withers <ch...@simplistix.co.uk>:


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
John Machin  
View profile  
 More options Oct 30, 7:16 pm
From: John Machin <sjmac...@lexicon.net>
Date: Fri, 30 Oct 2009 19:16:39 +1100
Local: Fri, Oct 30 2009 7:16 pm
Subject: Re: [pyxl] Re: Newbie: How to check for multiple command line arguments
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:

>> http://code.google.com/p/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):

HTH,
John


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
johannes rara  
View profile  
 More options Oct 30, 10:42 pm
From: johannes rara <johannesr...@gmail.com>
Date: Fri, 30 Oct 2009 13:42:06 +0200
Local: Fri, Oct 30 2009 10:42 pm
Subject: Re: [pyxl] Re: Newbie: How to check for multiple command line arguments
Thanks!

2009/10/30 John Machin <sjmac...@lexicon.net>:


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
John Machin  
View profile  
 More options Oct 30, 11:54 pm
From: John Machin <sjmac...@lexicon.net>
Date: Fri, 30 Oct 2009 23:54:09 +1100
Local: Fri, Oct 30 2009 11:54 pm
Subject: Re: [pyxl] Re: Newbie: How to check for multiple command line arguments
On 30/10/2009 10:42 PM, johannes rara wrote:

> Thanks!

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

Seems you "found" that on stackoverflow.

http://stackoverflow.com/questions/1643643/how-to-test-for-multiple-c...

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?

Cheers,
John


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google