This is my first Python program, getting data through json files to build scientific citations:
....
js = json.loads(data)
# here is an excerpt of my code:
print("Journal articles:")
for art in js['response']['docs']:
stuff = art['docType_s']
if not stuff == 'ART': continue
tit = art['title_s'][0]
nom = art['authFullName_s'][0]
jou = art['journalTitle_s']
dat = art['producedDateY_i']
try:
pbm = art['pubmedId_s']
except (KeyError, NameError):
pbm = ""
print(nom,', ', tit, '. ', jou, '.', dat, '. Pubmed: ', pbm, sep="")
# sample output: J A. Anderson, Looking at the DNA structure, Nature, 2018. Pubmed: 3256988
It works fine so far, except that I don't know how to hide key values from the print statement when keys have no value (ie. there is no such key in the json file for one specific citation).
For example, some of the scientific citations have no "Pubmed" ID key/value (pmd). Instead of printing "Pubmed: " with a blank value, I would like to get rid of both of them:
# Desired output (when pbm key is missing from the JSON file):
# J A. Anderson, Looking at the DNA structure, Nature, 2018.
# NOT: J A. Anderson, Looking at the DNA structure, Nature, 2018. Pubmed:
I tried the following (don't print pmd if value is empty) but it doesn't work:
print('. Pubmed: ', pbm if pbm != "")
Thanks for your help.