Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

how to extract dictionary value from string in python

import ast
temp = "'mycode':['1','2','firstname','Lastname']"
key,value = map(ast.literal_eval, temp.split(':'))
status = {key: value}
Comment

extract values from dict python

>>> d = {1:-0.3246, 2:-0.9185, 3:-3985}

>>> d.values()
<<< [-0.3246, -0.9185, -3985]
Comment

how to extract values from a dictionary

import json

# I have assumed your dictionary like that
data = {"message":"{"_":"user","pFlags":{"contact":true},"flags":2175,"id":379951860,"access_hash":"6967195540985199805","first_name":"پژوا","last_name":"روزبهی","username":"mramtd2","phone":"989157145632","photo":{"_":"userProfilePhoto","photo_id":"1631880813210609625","photo_small":{"_":"fileLocation","dc_id":4,"volume_id":"448413446","local_id":476387,"secret":"655623158723369503"},"photo_big":{"_":"fileLocation","dc_id":4,"volume_id":"448413446","local_id":476389,"secret":"13993366131879811943"}},"status":{"_":"userStatusOffline","was_online":1558046876}}","phone":"989157145632","@version":"1","typ":"telegram_contacts","access_hash":"6967195540985199805","id":379951860,"@timestamp":"2020-01-26T13:50:12.793Z","path":"/home/user/mirror_01/users_5d65f610ec18aa615a5f580c.log","username":"mramtd2","type":"redis","flags":2175,"host":"ubuntu","imported_from":"telegram_contacts"}

# The data in "message" need to be loaded on JSON format to access it.
data["message"] = json.loads(data["message"])

# Now, you can print or whatever you want with all the data
print(data["message"]["id"])
print(data["message"]["first_name"])
print(data["message"]["last_name"])
print(data["message"]["phone"])  # There is phone info both in data["message"]["phone"] and data["phone"]. They are identical. You can use whichever you want.

# Optional
# If you want, you can assign data["message"] to a variable.
finalData = data["message"]

# Then you can access the info that you need from finalData
print(finalData["first_name"])
Comment

PREVIOUS NEXT
Code Example
Python :: how to create a string in python 
Python :: convert number to reversed array of digits python 
Python :: python plot speichern 
Python :: lose your django secret key 
Python :: pandas.DataFrame.fillna 
Python :: os dir exists 
Python :: ord python3 
Python :: sorted key python 
Python :: regular expression syntax python 
Python :: No installed app with label 
Python :: pd.merge duplicate columns remove 
Python :: run julia in p;ython 
Python :: ttktheme example 
Python :: matplotlib temperature celsius 
Python :: python datetime with day date suffix format 
Python :: re module documentation 
Python :: log in python 
Python :: python how to reversetty.setraw(sys.stdin) 
Python :: how to convert frame number in seconds python 
Python :: find an element using id in requests-html library in python 
Python :: python enumerate() 
Python :: Flatten List in Python Using NumPy concatenate 
Python :: add colorbar without changing subplot size 
Python :: how to print list without newline 
Python :: python arabic web scraping 
Python :: every substring python 
Python :: how to chose version of python 
Python :: what are postcondition errors in python 
Python :: python boucle for 
Python :: python text recognition 
ADD CONTENT
Topic
Content
Source link
Name
7+1 =