Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Python: Using Argparser

Posted on 2010-08-20
4
Medium Priority
?
1,269 Views
Last Modified: 2012-05-10
Using Argparser can I separate multiple command line arguments with a comma rather than a whitespace? Currently "my_script.py -i NY, Philly, Baltimore" gives ['NY, Philly, Baltimore'] instead of ['NY', 'Philly', 'Baltimore'].
0
Comment
Question by:abuhaneef
4 Comments
 
LVL 25

Expert Comment

by:clockwatcher
ID: 33493737
Can you post your code?  And explain more what you want to do?   Do you really want NY, Philly and Baltimore to be individual arguments or do you want them as options to your '-i' argument?  

Based on the command-line you posted (rather than what you asked), it seems like you'd want something like this (handles commas or spaces) and returns NY, Philly, Baltimore as a list of values for '-i':

import sys
import argparse
import re

parser = argparse.ArgumentParser()
parser.add_argument("-i", nargs="*")
args = parser.parse_args()
args.i = re.split(r'[\s,]+'," ".join(args.i))
print args.i


---------------------------------------------------------------------


If you really are asking to get the comma to count as a command-line separator-- I don't think that's what you really want to do but it's what you said you wanted to do--- then this would probably work:

arg = parser.parse_args(re.split(r'[\s,]+'," ".join(sys.argv[1:])))
0
 
LVL 17

Expert Comment

by:gelonida
ID: 33495906
I'd do argument parsing in two steps.

let the argument parser parse the fields separately and get "NY,Phily,..." in one variable.

use then split to split further.

should NY,Phily,..." all belong to command option ;-i; or is I independent?

If you don't know whether the user passes the argument separated with spaces or with ',', then you had to write slightly more complicated cocde.


Ideally you show usyour existing code snippet,
 the command line,
the expected result

 (From your expected result it's not really clear what happened with tthe '-i' for example.
0
 
LVL 15

Accepted Solution

by:
mish33 earned 500 total points
ID: 33597788
You can easily convert  ['NY, Philly, Baltimore'] to ['NY', 'Philly', 'Baltimore'] by
iargs = iargs[0].replace(',',' ').split()

Open in new window

0
 
LVL 3

Expert Comment

by:T1750
ID: 33670763
mish33 is the correct answer beceause it's simple, quick to use and solves your proble, you should accept it.
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show the steps for installing Python on Ubuntu Operating System. I have created a virtual machine with Ubuntu Operating system 8.10 and this installing process also works with upgraded version of Ubuntu OS. For installing Py…
Sequence is something that used to store data in it in very simple words. Let us just create a list first. To create a list first of all we need to give a name to our list which I have taken as “COURSE” followed by equals sign and finally enclosed …
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question