How to read excel in python from user input

I have an excel file that looks like this:

Bob | 15

Joe | 25

Tim | 19

I'm trying to write a code so that if the user inputs the name bob, it will print 15.. or if the user inputs Joe, it will print 25 etc

I can't figure out how to get python to read an input string and match it to the cell in excel

4 answers

  • answered 2020-07-05 04:56 Jeel Gondaliya

    Through pandas you can do this free feel to ask.

    import pandas as pd
    df = pd.read_excel(file_path)
    user_input = input()
    value = df[df['name_column'] == user_input]
    print(value)
    

  • answered 2020-07-05 05:00 Renato Aranha

    You can easily do it using only Pandas

    import pandas as pd
    
    # to read the Excel into Python:
    df = pd.read_excel("filename.xlsx")
    
    # to get input name from user
    input = input()
    
    # to get output id from input name
    output = df[df.name == input].id
    
    # to print result
    print(output)
    

  • answered 2020-07-05 05:03 shashank shekhar

    just follow these steps buddy to open excel in python

    import xlrd 
      
    
    tt = open("file name ") 
      
    #opening excel
    ex = xlrd.open_workbook(tt) 
    sheet = ex.sheet_by_index(0) 
      
    # for row 0 and column 0 
    print(sheet.cell_value(0, 0))
    

  • answered 2020-07-05 05:09 Christopher

    You probably need to consider what if the name user input doesn't exist.

    import pandas as pd
    
    df = pd.read_excel('test.xlsx', sheet_name='Sheet1', header=None)
    
    name = input('Input name: ')
    
    try:
        value = df[df[0] == name][1].values[0]
        print(f'value of {name} is: {value}')
    except IndexError:
        print('Name not found')
    

    Output 1:

    Input name: Bob
    value of Bob is: 15
    

    Output 2:

    Input name: Chris
    Name not found