I have a webpage, that contains information as:
[{'name':'Apple', 'q': 10},{'name':'Banana', 'q':9}]
How to parse this data from web page
I have a webpage, that contains information as:
[{'name':'Apple', 'q': 10},{'name':'Banana', 'q':9}]
How to parse this data from web page
This seems to be a list of dictionaries (mapping type), so to parse you can use:
l = [{'name':'Apple', 'q': 10},{'name':'Banana', 'q':9}]
for dictionary in l:
for key, value in dictionary.items():
print(key, value)
OUTPUT:
name Apple
q 10
name Banana
q 9
As stated John in a commnet, in case that is an string:
import json
s = "[{'name':'Apple', 'q': 10},{'name':'Banana', 'q':9}]"
l = json.loads(s.replace("'", '"'))
for dictionary in l:
for key, value in dictionary.items():
print(key, value)
Same output.
Seems you are dealing with a dictionary in Python. Lets assume the name of the dictionary be Nurislom_Turaev
. So to parse/print the elements you can do the following:
Nurislom_Turaev = [{'name':'Apple', 'q': 10},{'name':'Banana', 'q':9}]
for dictionary in Nurislom_Turaev:
for name,q in dictionary.items():
print (name, "=>", q)
Console Output:
name => Apple
q => 10
name => Banana
q => 9