I use anaconda's jupyter to run the code,
I use the requests module to read the web page,
see the output The content is within curly brackets , and it is judged to be a dictionary , so I use the dict function to read the value, but it fails.
type() Found that its attribute was found to be str
After I used json, I found that the attribute changed to dict.
When the program reads this type of content in dictionary formWhen strings are read,
how to make them become dictionary 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