1

I have an optionmenu working ok looking from a list in the code but what if the list is to be maintained externally in an excel file. How would I go about looking up an excel file, say called....vehicles.xlsx and the list would be in column A? Would I use pandas or there is ther an existing option within python The working code is below but obviously I want to get rid of the list and replace it with an excel lookup.

Sorry in advance if there is a tutorial somewhere but I can't seem to find it and I'm pretty new to python.

Thanks

self.cars=["car1","car2"] self.installin = tk.OptionMenu(self,self.var,*self.cars).grid(row=4,column=2,sticky=tk.W, padx=8, pady=8)

1 Answer 1

0

Checking out my Excel 2003, seems you can save spreadsheets as xml, txt, csv among others.

Not sure how the text file would look, but I'd start with that as it might save you a library import (xml, csv), in case that's a consideration.

Edit: In case it isn't, check out csvs' DictReader, I believe it's the quickest solution.

If you like/prefer to work with json, you can also convert csv to json with a few lines of code.

Edit 2: Extracting the first column from a csv file may look like this:

import csv

with open('install.csv') as csvfile:

    # Generating a DictReader object
    reader=csv.DictReader(csvfile)

    # This will contain the values in the 1st column
    first_col=[]

    # Assuming the first column is titled 'Ins' and skipping empty cells
    first_col.extend([row['Ins'] for row in reader if row['Ins']])

Alternatively, if you didn't name the columns, you can just use csv.reader. The relevant part then looks like this:

reader=csv.reader(csvfile)  # instead of csv.DictReader(csvfile)

first_col=[]

first_col.extend([row[0] for row in reader if len(row) and row[0]])
Sign up to request clarification or add additional context in comments.

3 Comments

Sorry, new to this,don't understand it yet. I've added the following with open('install.csv') as csvfile: self.reader = csv.DictReader(csvfile) for row in self.reader: read(row['Ins']) self.installin = tk.OptionMenu(self,self.var,*self.reader) I know its wrong, the example in the tutorial shows how to print but that's not what I need, is there a read function?
sorry for delay, didn't have a chance to try it out but I still can't get this to work. How would I apply this to the optionmenu tkinter object?
What we did is get a list of the items in the first column of a csv file. In your case, just use self.cars instead of first_col, and continue as you did before.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.