Python Forum

Full Version: Python VLookup? Lookup Table?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi All... can someone tell me an easy/efficient way in Python to create a lookup table that acts like Excel? My options are unlimited, but I cannot find just what I am looking for. I have a GUI where someone scans in a Badge# and I am writing that to a text file with other data. I need to either create a lookup table to get the persons name so that when I write the data, it shows both.

Example:
Badge Input Scan: 8923468
Needs to become: 8923468 (John Smith)

I can fill a simple text file like:
8923468, John Smith
I just need to find the simplest method for accomplishing this task and how to search the name like a VLOOKUP would do. Thanks
It looks like you need a database. A simple text file (in any format - csv, json, etc.) will work too, sort of (i.e. load the file in memory as a dict and the process the scans), but a simple database (even sqlite will do - support comes with python) will be much better.
Is this for during execution only or are you also asking about the equivalent of a spreadsheet that uses VLookup? For execution only you can use dictionaries. You could store the data as a CSV, as JSON or as XML and then build the dictionary from that. If you do that then a database might be easier overall.

So it depends on details not yet revealed. If there is more data (other fields, such as level of access) that is relevant then that makes a difference too. Of course, the stated requirements are simple enough that this sounds like a class assignment and if so then there are likely not much more about requirements.
thanks all for the suggestions on this. I will probably try one of these methods. To answer your question Sam Hobbs... Yes, this is something to lookup during execution. I am using a Tkinter GUI, and when they scan their badge number, I want to lookup their name, store to a variable and write it to a text file.