Enter values within Variable python

1
#!/usr/bin/python
# coding=utf-8
import time
from selenium import webdriver
import xlsxwriter
from Data_tags import Inventory_data

driver = webdriver.Chrome('/Users/Martin/Desktop/chromedriver')


def run(d):
    login("Usuario", "pass")
    NewTag("https://platform/en/users/inventory/create")
    Device = str(d.get("Li_platform")) #Mobile Web 
    Inicio_Li_Platform = ('//*[@id="platform_listbox"]/li[')
    Final_Li_Platform = (']')
    Platform(Inicio_Li_Platform+""+Device+""+Final_Li_Platform)
    NameTag(d.get("TagName"))
    Floor_Rate(d.get("Floor_price"))
    Save_Tag('save')
    Save_xlsx(d.get("Excel_name"))
    Inicio_Li_Publisher = ('//*[@id="publisher_listbox"]/li[')
    Final_Li_Publisher = (']')
    Publisher = str(d.get("Li_Publisher")) #Publisher Altitude
    Publisher_Li(Inicio_Li_Publisher+""+Publisher+""+Final_Li_Publisher)
    Tag_Rate_and_Save(d.get("Tag_Rate"))
    time.sleep(2)
    driver.close()

def main():
    while True:
        tag = input("Ingrese la clave deseada, nada para salir: ")
        if not tag:
            break  
        d = (Inventory_data.get("Inventario_datos")).get(tag, None)
        if d:
            run(d)
        else:
            print("Clave no encontrada.")

main()

This is a part of my code with which I can enter a value through the terminal which serves to execute my program. This value remains in my variable ( d = (Inventory_data.get("Inventario_datos")).get(tag, None) ). So far the program runs perfect, but I need to enter more than one value at a time. Every time I enter a value the program runs, what I am looking for is that the program be executed for each value that I enter in a single time. An example to be clearer would be to write in the terminal "auto", "truck", "bicycle" and that the program execute all functions for auto, once you finish with car, do the same with truck and so on with all the other values.

Thanks

    
asked by Martin Bouhier 09.10.2017 в 23:13
source

1 answer

1

You can capture a single string by separating each key by a suitable separator such as a comma or any character that is not allowed in the keys themselves. Then you apply str.split to separate each key and use it to execute the function by:

def main():
    tags = input("Ingrese las claves deseadas separadas por comas: ")

    for tag in (tag.strip() for tag in tags.split(",")):
        d = (Inventory_data.get("Inventario_datos")).get(tag, None)
        if d:
            run(d)
        else:
            print('Error: Clave "{}" no encontrada.'.format(tag))

The entry should be something like this:

  

car, truck, bicycle, truck

Edit:

In Python 2.x you have to change input by raw_input :

tags = raw_input("Ingrese las claves deseadas separadas por comas: ")
    
answered by 09.10.2017 / 23:37
source