-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

  • See: [How do I do X?](https://meta.stackoverflow.com/questions/253069/whats-the-appropriate-new-current-close-reason-for-how-do-i-do-x) The expectation on SO is that the user asking a question not only does research to answer their own question but also shares that research, code attempts, and results. This demonstrates that you’ve taken the time to try to help yourself, it saves us from reiterating obvious answers, and most of all it helps you get a more specific and relevant answer! See also: [ask] – JeffC Sep 01 '17 at 05:07
  • 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
  • 151,581
  • 34
  • 225
  • 281