Creating a Dictionary from CSV file with a single Key and Multiple Values

I've successfully been able to use CSVtoList to create and return key value pairs, but I'm currently trying to create part of an app that can take a CSV file and turn in into a dictionary, where the first column of a row would be the key, and the subsequent columns would be all the associated values. When I search for that key I could return the associated values. Take the following example of a sample CSV table:

Screenshot 2022-06-17 122712
Lookup.csv (117 Bytes)

I would like to convert that into a dictionary where I could search for the UPC key and return all the values for that row (description,cost,price), or possibly any value I choose, though I know the latter would be more complicated.

I've seen a lot of posts about dictionaries but they all seem to being doing something slightly different than what I need, and I'm having a tough time figuring it out. Is there possibly a better way even? Thanks for your help in advance.

From the sound of it you probably want a dictionary that looks like this?

image

or, if you do not need the "keys" in your csv

image


(but this mixes dictionaries with lists)

If not, draw out the json/dictionary structure you intend.

You may also find it easier to just work with lists, if you are comfortable with the structure of your list?

That structure looks like it might work and it makes sense when set up manually as you've done it. But I'm trying to import the CSV from a file and have it populate the dictionary with the UPCs set up as keys.

Here is an example of a screen that I'm working on:

I can handle the formatting of the output data, but really I just want it to output the rest of the row essentially when the UPC is entered. Does that clarify it? If what you showed me is correct and it's trivial to have the dictionary set up from a file instead of imputing all the entries manually, I'm just not seeing it. Thank you for your help thus far.

What do you want to happen when the user presses "Item Submit"?

You do not need to go to all the trouble of creating and working with dictionaries, you can do this with your csv table converted to an AI2 list.

image

Here the blocks get the data from the csv, then set a listpicker items with the UPCs
On selecting a UPC, the additional data can then be displayed ( I haven't done any formatting work on the three items)

Oh, I thought I might have be thinking about it the wrong way! This is exactly what I was trying to think of. I'm used to SQL so it's hard for me to not just be able to write a select from table where.... I will try this out and let you know how it works. Thanks so much for your help!

OK, I guess I'm still a little confused by your example. I don't really understand why there are 3 separate label.texts being set. Shouldn't it just be one for the output? The UPC is an input, and there would be one output as far as I can tell. Could you possible post an .aia file so I could see how it works?

Well, you could always use SQLite....

Taifun SQLite

or

Simple SQLite

Your previous example seemed like it would work, I just don't understand why there are so many labels being set without looking at an aia file. I'll look into the SQL lite options as well. It just seems like overkill for a simple key/row lookup for a single table essentially. I would have thought this would be trivial. I guess I'm a little out of my element.

This is trivial. I did add the listpicker into the UI which saves the user from typing in a UPC number.

I was just using labels to print out content whilst developing

OK, I know I'm being bothersome at this point, but listpicker for UPC is probably not an option. There will be upwards of 30,000 UPCs in the actual use of the program. Is this possible with a search? On the screenshot of the UI that originally posted, the lookup code triggers after something has been entered in to the UPC field and it loses focus.

No amount of searching is likely to help, with 30,000 entries. How will the user know which UPC to enter ?

Also with that number, you may want to consider SQLite, it is much faster than tinydb, or AI2 lists.

This sample will load your little table at startup, though I have no timing data for how long it would take for 30,000 rows.

If it's too slow, you could instead load TinyDB NameSpaces, one per column, tag = UPC code for all of them, just on the first run.
CSVToDict.aia (3.4 KB)


initialize global CSVText to (1)




Lookup.csv (117 Bytes)
Sample Data

This would be a person scanning an item and then having the product description pop up. The scanning aspect is already solved, the dictionary lookup is what I was concerned with. Thanks again for your help, I feel ABG has given me the correct solution.

Yeah, I actually tested it with a 60,000 row CSV file and the lookup on the device that I'm using took about .6 seconds, so I don't foresee that being too much of an issue. It's really more for the user to know whether or not the item has been set up in the back system that provided the CSV file in the first place. Your solution looks perfect. Thanks for your help.

Always good to know the objective form the offset :wink:

Thanks for the timing data!

Good point. I should have stated it. Will keep that in mind next time I'm reaching out.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.