-1

I have a webpage, that contains information as:

[{'name':'Apple', 'q': 10},{'name':'Banana', 'q':9}]

How to parse this data from web page

  • Possible duplicate of [Parsing values from a JSON file?](https://stackoverflow.com/questions/2835559/parsing-values-from-a-json-file) – JeffC Sep 01 '17 at 05:07

2 Answers2

1

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.

  • 1
    If it's contained in a webpage as stated, then it can't be a native Python dict; it would have to be a text representation of a dict. – John Gordon Sep 01 '17 at 04:42
  • @JohnGordon You are right. I edited the answer. Thanks –  Sep 01 '17 at 04:51
0

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
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352