How do you extract specific value for key from CSV that is converted into Dictionary

I stumble upon this posting on CSV to dictionary conversion. I try to extract specific value from the key. I can only think of segmenting the text value to what I want but this cause issue when the key is not found and if the value changes in length. Is there any other way so that I can pinpoint exactly to which column of value that I want? By the way I still want the error message "not found etc." in case wrong key is entered in the textbox.

For example I want to extract the value 'Description' or 'Cost' for specific key (ex 1234).


Regards.

This is the edited aia and original csv file.
CSVToDict.aia (3.8 KB)
Lookup.csv (117 Bytes)

Here's a fully dictionary based approach, using the CSV table headers to prompt for columns, and the column 1 header as a key name.
CSVToDictWithLookup.aia (6.7 KB)

sample run

I later added a trim() around the textbox.Text to avoid asking for keys with leading blanks.
I also added a YAML option to see the whole subdictionary in YAML format:

Expanded field selection option list:
Field choices

YAML display sample:
YAML display

Designer:
The List Picker lets you ask for single columns, based on the CSV headers, or ask for everything, in YAML format.

globals:
initialize global CSVText to



Startup:

When the CSV file arrives, we load the dictionary and prep the GUI:
We trim the headers, for more robust lookup.
We set up the Hint for the TextBox where we request the key, and clear its .Text to show the Hint.


Twi invisible Labels are on the screen in case we need to debug later.

Building the dictionary of dictionaries from the csv table:

I had to add length of list checking to bulletproof against a short row at the end of the file.
The top level result dictionary uses the column 1 value preceded by a 'K' as its key, to work around how the dictionary object will differentiate between string and number versions of the same key. Each value is a subdictionary mapping each header column text into that row's cell value for that header.

Lookup process:

Loading the List Picker of column headers:


I inserted an extra option [YAML] at the head of the field list.

Responding to a switch of column header:

The lookup button:
when  btnLookup .Click do

The lookup procedure:

Again, I had to add a 'K' prefix to the top level key to match the loaded dictionary.
Not Found detection is done in two phases:

  • high level key not found
  • low level key (from the List Picker) not found.

That allows for more customized error responses in the result Label.

If there was no specific field selected, or the new [YAML] option was selected, we show everything in YAML.

The YAML conversion procedure:


See above for a sample YAML output.

Please note that this is general purpose, knowing nothing about the CSV file except its name and that its key is in column 1.

I now have trimmed both the headers and the input to avoid my start-up not found anomalies.

2 Likes

You may use get cell value block:

simple and almost get it done. only issue is 'select list item' block sends out error cannot accept arguments when the 'get value for key' block returns 'not found'. any suggestion on how to handle the error?

forgot to mention the key (upc column for this case) that i will be using is in the format of 3 digits starting from 000 up to 999 only so each row will be unique and will not be the same with each other. also i prefer to select the value by using index rather than listpicker this is because the value will be shown in each individual label (which i will add extra notes or comments for it). the UI will be like as shown below,
UI example
the key (or UPC) not found checking is only needed in the case of incorrect textbox input that doesn't precisely match with any key in the list (ex. AB3 or test or blank)
TIMAI2 suggestion on using the select list item block almost get it done only if the key 'not found' error can be handle :sweat_smile:

wow interesting. this one I have to rework the blocks and test it out first

Ah yes, missed that....

I refurbished my prior sample to include YAML out options too.

YAML display

Issue solve by converting csv into list and use appropiate and correct way to do lookup from sublist generated.