Created
September 14, 2020 16:54
-
-
Save tushortz/395e265fa893266c3dbdb952fba30d17 to your computer and use it in GitHub Desktop.
An attempt to parse json or python dict using just normal strings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Author: Taiwo Kareem | |
# Attempt to parse dict by using string path | |
def str_data(paths, obj): | |
paths = paths.split(".") | |
for path in paths: | |
if path.isnumeric(): | |
path = int(path) | |
obj = obj[path] | |
return obj | |
# EXAMPLE USAGE | |
data = { | |
"players": [ | |
{ "player": "Jack Jones" }, | |
{"player": "Mike Dean", | |
"hobbies": [ | |
"swimming", | |
"dancing" | |
] | |
} | |
] | |
} | |
print(str_data("players.1.hobbies.1", data)) | |
# result: "dancing" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment