0

I use a module called scholarly.py

https://pypi.org/project/scholarly/

It works perfectly, but I cannot store the output from my query into any variable

Here is an exemplary case (same as in the website)

publication=scholarly.search_pubs_query('Perception of physical stability and center of mass of 3D objects')

To access the result I must use print(next('variable'))

print(next(publication))

###output
{'_filled': False,
 'bib': {'abstract': 'Humans can judge from vision alone whether an object is '
                     'physically stable or not. Such judgments allow observers '
                     'to predict the physical behavior of objects, and hence '
                     'to guide their motor actions. We investigated the visual '
                     'estimation of physical stability of 3-D objects (shown '
                     'in stereoscopically viewed rendered scenes) and how it '
                     'relates to visual estimates of their center of mass '
                     '(COM). In Experiment 1, observers viewed an object near '
                     'the edge of a table and adjusted its tilt to the '
                     'perceived critical angle, ie, the tilt angle at which '
                     'the object …',
         'author': 'SA Cholewiak and RW Fleming and M Singh',
         'eprint': 'https://jov.arvojournals.org/article.aspx?articleID=2213254',
         'title': 'Perception of physical stability and center of mass of 3-D '
                  'objects',
         'url': 'https://jov.arvojournals.org/article.aspx?articleID=2213254'},
 'citedby': 15,
 'id_scholarcitedby': '15736880631888070187',
 'source': 'scholar',
 'url_scholarbib': 'https://scholar.googleusercontent.com/scholar.bib?q=info:K8ZpoI6hZNoJ:scholar.google.com/&output=citation&scisig=AAGBfm0AAAAAXIjCFpwk1u0XEARPUufLltWIPwQg4_P_&scisf=4&ct=citation&cd=0&hl=en'}

However, when it comes to JSON, it still doesn't work:

json.load(publication)
Traceback (most recent call last):

  File "<ipython-input-43-a51cc3f613b0>", line 1, in <module>
    json.load(publication)

  File "C:\ProgramData\Anaconda3\lib\json\__init__.py", line 293, in load
    return loads(fp.read(),

AttributeError: 'generator' object has no attribute 'read'

I tried to use other means such as

[How to make a class JSON serializable

And the result is:

AttributeError: 'generator' object has no attribute 'dict'

Don't know what else to do...

Mikev
  • 2,012
  • 1
  • 15
  • 27
  • This is because publication is a generator which generates a `dict` at each call, what do you try to achieve? As this is already a `json`? Are you trying to save it to a file? – BlueSheepToken Mar 13 '19 at 09:27

1 Answers1

0

Your generator (i.e. publication) generates a dictionary object.

In python, at least, you could say dictionary is the in-memory function of json and usually json refers to a file or string following the key:value structure. Using your dictionary you can straight away access the value via key like below.

a_dictionary = next(publication)
print(a_dictionary['citedby']) # this will output 15 on that particular data

If you really want to convert it to json for some reason, you could do it as below.

json_dump = json.dumps(a_dictionary) # convert dictionary to json string object
loaded_json = json.loads(json_dump)
loaded_json['citedby'] # this will also output 15

Hope this helps.

Darren Christopher
  • 3,893
  • 4
  • 20
  • 37