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 3 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.

egghead
egghead
~ 31 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today