Parse Data Into a Dictionary

My attempt isn't working can you help?

I have a variable created via python, that I want to parse into a dictionary for used as Keyboard Maestro variables. My feeble attempt only captures the first 'record' and none of the subsequent. The wiki and forum posts only show manual data entry into a dictionary. I'd like to automate it.

Is the data in the ie_dict.txt file formatted correctly? Are their other options?
Maybe I need to do some dictionary setup first?
I think, ideally I'd like to end up with a dictionary that used the first line as the key and then the rest of the 'record' as the value.
Ultimately I plan on adding a Custom HTML Prompt that displays the records in a tabbed format.

Here is my macro.

IE Dictionary.kmmacros (2.9 KB)
Here is the output of the 'ie_dict' variable.
ie_dict.txt.zip (1.5 KB)

You might want to look at exporting as JSON instead, then reading it into your Dictionary with the Set Dictionary to JSON action. But I'm no good with JSON, so here's a way to do it by parsing the text:

Parse to Dictionary.kmmacros (9.6 KB)

Image

Your "records" are separated by the character, so we treat it as an array split on that and iterate through from the second item (because the first, before that first star, is an empty element) to the last (the [0] item of an array is its length).

We "trim" the text of each item to remove the trailing empty lines, then use a regex to split it into everything from the start to the first linefeed -- the key -- and everything from after the first linefeed to the end -- the value -- and add that key/value pair to our dictionary.

Finally, show each key/value pair in its own window to show we've done what we wanted.

@Nige_S, your response is so helpful on many levels. Thank you.

I, too, could improve at JSON, but this might be an opportunity for me to learn.
Your solution is powerful, and I want to understand it, so I have a couple of questions.

  1. I've never seen a variable presented as "%Variable%Local_theText[Local_i]★%." Can you clue me into the meaning? Does this variable specifies theText delimited by the ★ for the array Local_i all in one line, as we'd say in python programming?
  2. Your use of the Filter Text action reminds me of many actions I've never used or thought of using. Instead, I've struggled with workarounds.
  3. This last point needs to be explained. "Parse to Dictionary" works with fabulous perfection for the scenario I described where the key is the first line (Title + UID), but when I change the text, so the first line is just the UID, it still prints the key as Title + UID. I need to find where Keyboard Maestro is getting keyName. If I change Local_keyName to Local_key, I get better behavior, but it is still incorrect, as the key is the same UID (the last one) on all records. When I look at the dictionary, it appears to be in order with the UIDs as keys, but it doesn't print in the display text window. What am I missing?

image

Yep -- see the "How to Use Custom Array Delimiter" section of "Using Variables" in the manual.

You'll need to show your new data, and upload your new version of the macro if you've made changes. And remember that Dictionaries persist (unless you clear them), so if you run the macro once "old-style" then make a change and run it "new-style" you'll have both old and new key/value pairs.

I'm not 100% sure about KM's implementation, but Dictionaries are general unordered collections of key/value pairs -- the sorting you see above is likely imposed by the Variable Inspector. So it's very possible that you can see new-style pairs first in the Inspector but the old-style ones are the first to be accessed in the "For Each" and so presented in "Display Text".

Local_key is used to process text into the Dictionary -- after that's done it will retain the value of the last key (until the end of the macro). Local_keyName is used to iterate through the Dictionary, outputing each key/value pair in turn. If you change Local_keyName to Local_key you need to change both the first, "For Each", box of the "For Each" action and every occurrence in the "Display Text" action.

Thank you again for your response.

@Nige_S, thanks for the reference to the "Using Variables" section in the manual. Looking at the manual makes me think that one could become one of the top 10% of the Keyboard Maestro maestros just by reading and studying the manual. I'm currently in the 90% who still need to read the entire v.10 manual.

This explains my confusion. Stupid me had thought that a %Dictionary% was like a %Variable% in that every time you set it, it overwrote the old value. As you explained it, with a %Dictionary%, values get concatenated. I can confirm this. The new data set does print in the "Display Text" windows. There are so many windows that the new ones are hidden. The new ones were printed first in a cascade of windows and were covered by the old ones making me think the new ones hadn't been printed.

I need to reset the dictionary so it can accept new records. The new records come from the output of a python script that searches and sorts 3000+ files and creates the record output. The data output will be different at each runtime.

In Python, it is easy to set a blank dictionary dict_ie = {}. I can't find anything about how to clear or reset a dictionary in the wiki or the forums. It could be my searching skills. Can you help?

You don't directly set a Dictionary, you set key/value pairs in a Dictionary. If the Dictionary doesn't exist it is created and the key/value pair added. If the Dictionary exists but the key is not present, the key/value pair is added. If the key already exists it is updated with the new value.

image

(This looks like it goes against the "You don't directly set a Dictionary", above. But you aren't -- you're setting the keys to null, effectively removing them, and KM garbage collects the empty Dictionary for you.)

I only stumbled across this when working on the above and looking for a way to reset the Dictionary between runs -- props to @kvanh for this post.

...and the Forum, and by simply messing around in KM trying to do things. I'm still with you in the 90% -- but working on it!

1 Like

Well, in a way it is like that. Every time you overwrite the value that is stored in a key of the Dictionary that unique slot in the Dictionary does get overwritten.

Think of the Dictionary as a collection of an infinite amount of Variables - each one designated by a unique key.

Here is a simple Macro that shows how the slot defined by a key called "1" of a Dictionary called "DCT__Test" can be used exactly like a Variable.

Click to Show Image of Macro

EXAMPLE Set a Value to a Dictionary Key and Access it.kmmacros (4.2 KB)

1 Like