pythonpandasdata-science

How can I write some code that ask to user to load a file


I'm building a tool for the company where I work and I've built a program that returns different statistics from any csv file, of course with an specific data structure. Now, my issue is that I don't know how to do is to request the user to upload a file. In order to create this program I've been using as practice mode,

this: df = pd.read_csv('',delimiter=';', encoding='ISO-8859-1')

Any ideas?


Solution

  • If you need some basic ui that the user can interact so they can select a file, you may want to consider the easygui module.

    Or if you don't want to install a new module, just use tkinter built in.

    Try this:

    from tkinter import *
    from tkinter.filedialog import askopenfilename
    import pandas as pd
    
    Tk().withdraw()
    print("Please select a csv file to load") 
    file = askopenfilename()
    df = pd.read_csv(file, header = 0)
    

    Hope this helps :)) . Check out the easygui module too. The documentation is brief so you can get a hang of it after a few tries.