1

I am trying to create a script that takes multiple command line arguments of the same type, and then feeds them into a for loop for processing, the below is what I have in my code:

import argparse

parser = argparse.ArgumentParser(description='XYZ')
parser.add_argument('[values]', nargs='+', help='A list of values')

u = parser.parse_args(['3crlV5QaiuUjRz5kWze', 'F9Xw0rggHZ_PLs62q'])

for values in u:
    ... # do a bunch of stuff for each "value"

I understand that the values stored in parse_args are a NAMESPACE, hence the error below when I run my code:

TypeError: 'Namespace' object is not iterable

Is there any way to turn them into a list object? so that I can pass them to my for loop, or is there any other approach I should be using? Just getting started with python, so apologies in advance if this is a noob question

1
  • What type of values do you want to iterate? Commented Jan 23, 2017 at 3:57

2 Answers 2

2

You need to iterate u.values (the argument name you specified in add_argument call) not u itself; To do that you need to rename [values] to values:

import argparse

parser = argparse.ArgumentParser(description='XYZ')
parser.add_argument('values', nargs='+', help='A list of values')
#                   ^^^^^^^^

u = parser.parse_args(['3crlV5QaiuUjRz5kWze', 'F9Xw0rggHZ_PLs62q'])

for value in u.values:
    #        ^^^^^^^^
    # do a bunch of stuff for each "value"
Sign up to request clarification or add additional context in comments.

1 Comment

A note in case anybody else comes along this question, though the question is tagged python3.x (and mentions python3.6) this solution works in python2.7+
0

The values from parsing are in a simple of object of class argparse.namespace (which can be customized - see the docs).

Usually you access those values by attribute name, e.g. args.foo. For unusual names you many have to use getattr(args, '[values]').

In the simple case argument values are a single string or number. With nargs like '*' or '+', the values will be in a list. With append action type you may even get lists within lists.

I encourage users to print the args namespace.

In [2]: parser = argparse.ArgumentParser(description='XYZ')
   ...: parser.add_argument('[values]', nargs='+', help='A list of values')....
In [3]: args =parser.parse_args(['3crlV5QaiuUjRz5kWze', 'F9Xw0rggHZ_PLs62q'])

In [4]: print(args)
Namespace([values]=['3crlV5QaiuUjRz5kWze', 'F9Xw0rggHZ_PLs62q'])

With your 'dest' I have to use the getattr to fetch the list of strings from args.

In [5]: getattr(args,'[values]')
Out[5]: ['3crlV5QaiuUjRz5kWze', 'F9Xw0rggHZ_PLs62q']

args is a simple object, so you can add attributes or modify existing ones. e.g.

In [6]: args.values = ['3crlV5QaiuUjRz5kWze', 'F9Xw0rggHZ_PLs62q']
In [7]: print(args)
Namespace([values]=['3crlV5QaiuUjRz5kWze', 'F9Xw0rggHZ_PLs62q'], 
    values=['3crlV5QaiuUjRz5kWze', 'F9Xw0rggHZ_PLs62q'])

If you are more familiar with dictionaries, you can use vars():

In [9]: vars(args)['values']
Out[9]: ['3crlV5QaiuUjRz5kWze', 'F9Xw0rggHZ_PLs62q']

Play around with this sort of thing an interactive Python session.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.