-1

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
sirimiri
  • 509
  • 2
  • 6
  • 18

2 Answers2

0

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

Arpit 290
  • 1
  • 3
  • is not a dictionary, the problem here is to convert a string dictionary to a dictionary. Found out that below 2 lines worked import json result = json.loads(string) – sirimiri Sep 08 '21 at 06:31
  • hey i have fixed it so you can use this now – Arpit 290 Sep 14 '21 at 04:32
0

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)
sirimiri
  • 509
  • 2
  • 6
  • 18