I have a string (string dictionary) as below, with key and value pairs. How do I extract the 'text' information out using Python. Extracted_data = xxxxxx
data = '{"Created":"Wed", "id":1435,"text":"xxxxxx"}'
print(type(data)) = string
I have a string (string dictionary) as below, with key and value pairs. How do I extract the 'text' information out using Python. Extracted_data = xxxxxx
data = '{"Created":"Wed", "id":1435,"text":"xxxxxx"}'
print(type(data)) = string
You can just do
from json import loads
data = """
{"Created":"Wed", "id":1435,"text":"xxxxxx"}
"""
data = loads(data)
text = data["text"]
created = data["Created"]
id = data["id"]
print(id)
print(created)
print(text)
Outputs
1435
Wed
xxxxxx
So basically you are converting the string to the dictionary
https://www.geeksforgeeks.org/python-convert-string-dictionary-to-dictionary/
Here is what is working:
import json
# initializing string
test_string = '{"Nikhil" : 1, "Akshat" : 2, "Akash" : 3}'
# printing original string
print("The original string : " + str(test_string))
# using json.loads()
# convert dictionary string to dictionary
res = json.loads(test_string)