0

So I'm trying to create a very basic python UI where a user uploads a CSV with a list of cities and the program creates an inner join with a pre-existing database of zip codes returns a list of cities and their corresponding zip codes. My code is functional so far. I'd just like the user to be able to upload a CSV from anywhere in their system. Right now, the program only reads from the python directory. I don't want to have to specify the file path of the input file. Is there any way this can be done? Here's the code I have right now -

import tkinter as tk
from tkinter import filedialog
import pandas as pd
root= tk.Tk()
canvas1 = tk.Canvas(root, width = 300, height = 300, bg = 'lightsteelblue2', relief = 'raised')
canvas1.pack()


def getCSV():
    global cities

    import_file_path = filedialog.askopenfilename()
    cities = pd.read_csv('cities.csv')
    zips = pd.read_csv('zips.csv')
    output = pd.merge(cities, zips, on='state' and 'county', how='inner')
    output.to_csv('output.csv', encoding='utf-8', index=False)

browseButton_CSV = tk.Button(text="      Upload City Data & Close     ", command=getCSV, bg='green', fg='white',
                             font=('helvetica', 12, 'bold'))
canvas1.create_window(150, 150, window=browseButton_CSV)

root.mainloop()

I'm kinda new to python and programming in general. Just been learning it over the past month or 2. Any help is appreciated!

Thanks, DJ

  • Possible duplicate of [Quick and easy file dialog in Python?](https://stackoverflow.com/questions/9319317/quick-and-easy-file-dialog-in-python) ... or [Choosing a file in Python with simple Dialog](https://stackoverflow.com/questions/3579568/choosing-a-file-in-python-with-simple-dialog) – wwii Nov 04 '19 at 19:55
  • This might be helpful: [Opening File (Tkinter)](https://stackoverflow.com/questions/16429716/opening-file-tkinter/16431848) – Zeeshan Nov 04 '19 at 19:57
  • Isn't the import_file_path the path to the csv file you want to open. Just pass import_file_path to pd.read_csv – Gingerhaze Nov 04 '19 at 20:17
  • @Gingerhaze - Thanks! I get the following error though - Can only merge Series or DataFrame objects, a was passed. It prompts me to add in a path to pd.read_csv – Dunanjay Chundur Nov 04 '19 at 20:28
  • @Gingerhaze I figured it out thank you! – Dunanjay Chundur Nov 04 '19 at 20:36

1 Answers1

0

@gingerhaze's answer -

import tkinter as tk
from tkinter import filedialog
import pandas as pd
root= tk.Tk()
canvas1 = tk.Canvas(root, width = 300, height = 300, bg = 'lightsteelblue2', relief = 'raised')
canvas1.pack()


def getCSV():
    global cities
    import_file_path = filedialog.askopenfilename()
    cities = pd.read_csv(import_file_path)
    zips = pd.read_csv('zips.csv')
    output = pd.merge(cities, zips, on='state' and 'county', how='inner')
    output.to_csv('output.csv', encoding='utf-8', index=False)

browseButton_CSV = tk.Button(text="      Upload City Data & Close     ", command=getCSV, bg='green', fg='white',
                             font=('helvetica', 12, 'bold'))
canvas1.create_window(150, 150, window=browseButton_CSV)

root.mainloop()