python - How to convert string to dictionary?
阿神
阿神 2017-06-22 11:52:19
0
2
801

When saving data, the format becomes this mode. There are many such data:

{"status": "0", "msg": "ok", "result": {"name": "Dew", "content": "<p>release name
When the autumn dew is heavy, go to the flowers and plants in the morning to collect it.</p><p>Smell
Sweet, flat and non-toxic.</p>

Stop thirst. Baihua Dew: Can make the skin healthy. Cypress Leaf Dew, Calamus Dew: Wash your eyes every morning, can enhance vision.
Leek Leaf Dew: Treat vitiligo. Apply to the affected area every morning. </p>", "commentary": "", "translation": "", "appreciation": "", "interpretation": ""}}
{"status": "0", "msg ": "ok", "result": {"name": "明水", "content": "<p>
世名

is also called Fangzhushui. Fangzhu is one The name of the big clam. On a moonlit night, catch Fang Zhu and take the water stored in its shell. If it is clear and pure, it is Fang Zhu water.</p><p>
Smell Sweet, cold, non-toxic.</p><p>
Indications Used to wash eyes, it can remove fog and improve eyesight. Drinking this water also has a calming effect and can also relieve children's troubles. Hot.</p>", "commentary": "", "translation": ""}}
After type, the format is displayed as a string. I used the built-in function, but it still failed to convert it to the standard dictionary format, as follows:

from pprint import pprint
import json
import requests
with open('bencao_detail','r',encoding='utf8') as file:
    str_file = file.read()
str_file

The output results are as follows:

'{"status": "0", "msg": "ok", "result": {"name": "Dew", "content": "<p> Name
When the autumn dew is heavy, go to the flowers and plants in the morning to collect it.</p><p>Smell
Sweet, flat, non-toxic.</p> ;<p>Indications
Used to decoct the medicine to moisten the lungs and kill insects, or mix the powder for treating scabies and worms into topical medicine, which can enhance the curative effect.
White Flower Dew : Stop thirst.
Baihua Dew: Can make the skin healthy.
Cypress Leaf Dew, Calamus Dew: Wash your eyes every morning, can enhance vision.
Leek Leaf Dew: Treat vitiligo. Apply to the affected area every morning .</p>", "commentary": "", "translation": "", "appreciation": "", "interpretation": ""}}n{"status": "0", "msg" : "ok", "result": {"name": "明水", "content": "<p>世名
is also called Fangzhushui. Fangzhu is a kind of The name of the big clam. On a moonlit night, catch Fang Zhu and take the water stored in its shell. If it is clear and pure, it is Fang Zhu water.</p><p> Smell
Sweet , cold, non-toxic.</p><p>Indications
Used to wash eyes, it can remove fog and improve eyesight. Drinking this water also has a calming effect and can also relieve irritability in children. .</p>", "commentary": "", "translation": ""}}n{"status": "0", "msg": "ok", "result": {"name": "Winter Frost", "content": "<p>release
How to get the frost: sweep it with chicken feathers, put it into a bottle, seal it and store it in a cool place. Even if it turns into liquid, Li</p><p>Smell
Sweet, cold, non-toxic.</p><p>Indications
Drinking winter frost can To relieve hangover fever, if you feel hot and flushed ears after drinking, drink it and it will disappear immediately. For typhoid fever and nasal congestion, drinking winter frost can also clear your nose. For prickly heat and redness and swelling under the armpits, apply winter frost and clam powder, which is effective. For malaria caused by cold and heat, you can treat autumn frost or winter frost for one and a half qian, along with hot wine, and it will also be effective. </p>", "commentary" : "", "translation": ""}}n
How can I make it a dictionary one by one?

阿神
阿神

闭关修行中......

reply all(2)
phpcn_u1582

In fact, eval is the simplest in this standard dictionary format.

t='''{"status": "0", "msg": "ok", "result": {"name": "露水", "content": 
"<p>释名在秋露重的时候,早晨去花草间收取。</p><p>气味甘、平、无毒。</p><p>主治用以煎煮润肺杀虫的药剂,或把治疗疥癣、虫癞的散剂调成外敷药,可以增强疗效。白花露:止消渴。百花露:能令皮肤健好。柏叶露、菖蒲露:每天早晨洗眼睛,能增强视力。韭叶露:治白癜风。每天早晨涂患处。</p>", "commentary": "", "translation": "", "appreciation": "", "interpretation": ""}}'''

a=eval(t)
巴扎黑

You all know import json, why don’t you ask questions

import json

with open('bencao_detail','r',encoding='utf8') as file:
    str_file = file.read()
    dict_data = json.loads(str_file)
    
    // 或者
    
    dict_data = json.load(file)
    
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!