2

I want to add arg 1 to a range of numbers while generating in python:

for arg in sys.argv[1]:
    for i in xrange(0,200000):
        print "%s%05d"%(arg, i)

But this is not working...

I want argv[1] just be a number!

For example:

python script.py 012345
01234500000
01234500001
01234500002
.
.
.
5
  • If argv[1] will just be number, how you will iterate over it. Because iteration is only possible over a sequence in python. And integer number is not a sequence. Commented Jan 5, 2015 at 8:38
  • Your question is unclear: is that first argument supposed to be an offset to the range? Commented Jan 5, 2015 at 8:39
  • @Evert Please see my question is updated... argv[1] may starts with 0 or not... but the point is sys.argv[1] must be only number Commented Jan 5, 2015 at 9:20
  • But do you want to add it to i, or do you want to append to (the string version of) i? Your first sentence says "I want to add ...", but your code shows "append". The reason is that if the former, your code can be quite different. Commented Jan 5, 2015 at 9:23
  • @Evert ... I want to append I mean/// So how can I get my desired output...I want numbers only..but when I use number started with 0 it won't show me because of int type...if I use str... so if I say 0123dd as arg 1 it would also generating results for me...and this is not what I want Commented Jan 5, 2015 at 9:26

4 Answers 4

4

Its good practice to have a check for your argv length.

EDIT

I've modified the solution to only allow numeric arguments but keep pre-leading zeros.

if len(sys.argv) == 2 and str(sys.argv[1]).isdigit():
    for i in xrange(0,200000):
        print "%s%05d"%(str(sys.argv[1]), i)
elif len(sys.argv) == 2 and not str(sys.argv[1]).isdigit():
    print ("Given argument must be digit!")
else:
    print ("Wrong number of arguments")
Sign up to request clarification or add additional context in comments.

8 Comments

It has a problem... When I say 0123dd as sys.argv[1] it is working... but I don't want it... @Duncan
use int(sys.argv[1]) for integers
No..I want get just numbers
Is your input a fixed length? if so you can pad it too, print "%05d%05d"%(int(sys.argv[1]), i)
Please review the updated answer, it should suffice.
|
1

You can use a try/except block to make sure the argument is both present and a valid integer, then proceed from there...

import sys

try:
    base = int(sys.argv[1])
# Handle where argument is not present or not an integer
# Either exit, set a default value or whatever
except (ValueError, IndexError):
    raise SystemExit('must supply an integer value as first argument')

# Do whatever
N = 20
for num in xrange(N):
    print '{0:05}{1:05}'.format(base, num)

Comments

1
  1. As we are accepting string as number then do type conversion from string to int and handle exception if user give wrong input string e.g. 0112eweee
  2. If we want output as String and do not want to strip 0 from string(beginning) and use string format method.
  3. If we want output in int then use mathematics process to create number. Function addByMaths() does maths operation.

Code:

import sys
def addByMaths(arg):
    arg *= 100000
    print "=========Maths========"
    for i in xrange(0,10):
        print arg+i

def appendByString(arg):
    print "=========String========"
    tmp = arg+"%05d"
    for i in xrange(0,10):
        print tmp%(i)
try:
    arg_o = sys.argv[1]
    arg_i = int(arg_o)
    #- Add according to maths.
    addByMaths(arg_i)
    #- Append by String method 
    appendByString(arg_o)
except:
    print "Invalid input."

Output:

python test.py 01
=========Maths========
100000
100001
100002
100003
100004
100005
100006
100007
100008
100009
=========String========
0100000
0100001
0100002
0100003
0100004
0100005
0100006
0100007
0100008
0100009

Comments

0

Below code should do the trick. sys.argv[1] contains the string value "123456", with your first for loop you're iterating over the different parts of the string.

def isint(s):
    try: 
        int(s)
        return True
    except ValueError:
        return False

arg = sys.argv[1]
if isint(arg):
    for i in xrange(0,200000):
        print "%s%05d"%(arg, i)

4 Comments

Thank you... Good answer but when I set 0123456.. it won't print 0 in result...
If user string value is "012345" and you do not want to strip 0 then do not convert string value to integer value . just like arg = sys.argv[1]
@Vivek Sable ok..but you see I can say 0123dd and script works... I just want number be accepted
yes, correct. good to use type conversion. if you are accepting string of digits. but finally code is print string.

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.