I use anaconda's jupyter to run the code,
I use therequestsmodule to read the web page,
see the output The content is withincurly brackets, and it is judged to be adictionary, so I use thedict functionto read the value, but it fails.
type()Found that its attribute was found to bestr
After I usedjson, I found that the attribute changed todict.
When the program reads this type ofcontent in dictionary formWhenstringsare read,
how to make them becomedictionary attributes?
Please use the <> edit button to add code when asking questions in the future, so that others can try the code.
Try the following code:
The result should be that both dictionaries have the same content. In other words:
x is to execute the string of r.text directly as expressions to generate a dictionary
y is the json object returned by the r.json() method, which generates a dictionary
So your question is:
"When the program reads this type of dictionary content as a string, how to make it a dictionary attribute again?"
You can change the question more accurately to:
"String is an expression in the form of a dictionary. How to turn a string into a dictionary? "
Then the answer is the built-in function eval()
Of course, the requests module already has the .json() method, you can use it
d = r.json()
In this way, you will get dictionary d
There are still quotation marks outside