[Tutor] array input from keyboard

Oliver Schmitz oliver.schmitz at godaddy.com
Fri Sep 15 18:07:31 EDT 2017


Hi Derek,

you are iterating over the input string, this will result in single
chars in each iteration of your for loop.

You could use `split` on your string to split the string into a list,
splitted by comma.

Instead of:

>    vols = []
>    vols = input("Please enter your volume ids, comma delimited? ")
>    for vols in vols :
>        vols = vols.rstrip()

Do the following:

vols = input("Please enter your volume ids, comma delimited? ")
for vol in vols.replace(" ", "").split(","):
    # dome something with your vol

The creation of an empty list isn't needed.

You shouldn't name the element of the iteration the same as the object
you are iterating on. So instead of `for vols in vols` use for `vol in
vols` as inside the loop you are working on a single volume instead of
multiples.

The `replace` will remove all your whitespace (as it seems your strings
don't need/dont' want them). Next the `split` will split your input
elements comma seperated into a list for the iteration by the `for` loop.


Am 15.09.17 um 22:50 schrieb Derek Smith:
> I need to accept input from the user then store in an array/list.  This is my 1st python script.
> 
> #!/usr/bin/env python
> # Derek Smith
> # 09/2017
> # accept volumes for TSM for tape mgmt.
> 
> import os
> import sys
> 
> nput1 = ""
> nput2 = ""
> nput1 = input("\nIs your input 'file' based or 'cli' based? ")
> 
> if nput1 == "file" :
>     nput2 = input("Please provide your input file? ")
>     nput2 = nput2.lower()
>     print (nput2)
>     fh = open(nput2,"r")
> 
>     for ln in fh:
>         ln = ln.rstrip()
>         os.system("/usr/bin/dsmadmc -id=dereksmith -password=x -dataonly=yes move drm %s" %ln, "tost=onsiter")
>         os.system("/usr/bin/dsmadmc -id=dereksmith -password=x -dataonly=yes move drm %s" %ln, "wherest=vaultr tost=onsiter")
> 
> elif nput1 == "cli" :
>     vols = []
>     vols = input("Please enter your volume ids, comma delimited? ")
>     vols = vols.upper()
>     for vols in vols :
>         vols = vols.rstrip()
>         print("/usr/bin/dsmadmc -id=dereksmith -password=dereksmith -dataonly=yes move drm %s" %vols, "tost=onsiter")
> elif
>     print ("Incorrect input, exiting.")
>     sys.exit(99)
> 
> 
> __OUTPUT__
> 
> # python tsm_moveVR_tonsiter.py
> 
> Is your input 'file' based or 'cli' based? cli
> Please enter your volume ids, comma delimited? r20344l5,r20355l5
> /usr/bin/dsmadmc -id=dereksmith -password=x -dataonly=yes move drm R tost=onsiter
> /usr/bin/dsmadmc -id=dereksmith -password=x -dataonly=yes move drm 2 tost=onsiter
> /usr/bin/dsmadmc -id=dereksmith -password=x -dataonly=yes move drm 0 tost=onsiter
> /usr/bin/dsmadmc -id=dereksmith -password=x -dataonly=yes move drm 3 tost=onsiter
> /usr/bin/dsmadmc -id=dereksmith -password=x -dataonly=yes move drm 4 tost=onsite
> ...
> ...
> ...
> 
> Its printing each element per line.  I have tried various changes, read online help but gave up looking.
> I need it to print as below:
> 
> 
> /usr/bin/dsmadmc -id=dereksmith -password=x -dataonly=yes move drm R20344L5 tost=onsiter
> /usr/bin/dsmadmc -id=dereksmith -password=x -dataonly=yes move drm R20355L5 tost=onsiter
> 
> Thank you!!
> Derek Smith  |  Unix/TSM Administrator  | Racksquared Data Centers
> ::  dereksmith at racksquared.com  *: www.racksquared.com<http://www.racksquared.com/> |  www.racksquared.jobs<http://www.racksquared.jobs/>
> 
> [cid:image003.png at 01D2E9AA.1B9CF8F0]
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
> 

Regards,
Oliver Schmitz




More information about the Tutor mailing list