1. 1
    Turning a key/value string into a dictionary in Python
    1m 10s

Turning a key/value string into a dictionary in Python

Chris Biscardi
InstructorChris Biscardi
Share this video with your friends

Social Share Links

Send Tweet
Published 5 years ago
Updated 4 years ago

Sometimes you get strings instead of objects and you have to parse them out. Here's an example where the raw data I got from a Twitch chatbot message wasn't useful to me until I parsed it into a dictionary.

Chris Biscardi: [00:01] Let's start by opening a Python ripple. Next we'll assign the variable raw data to our string. I happen to get this string from a Twitch bot. Note that there are key value pairs, nothing is quoted and all the key value pairs were separated by a semicolon. First, we'll want to split the string on the semicolon.

[00:25] Next, for each item in the resulting list, we want to split on the equal sign. Finally, we want to construct a dictionary. We'll also set the result to items. Note that items is now a dictionary. Because items is a dictionary, we can now get any key in that dictionary. Here it seems that the subscriber value is a one wrapped in a string. We would want to deal with that if we cared about this value.

[01:07] Here's the username that sent the message.