简体   繁体   中英

How to fetch data from a piece of json file

I have multiple piece text from json For example

{"citationID":"a1u01nqrgpa","properties":{"formattedCitation":"[2]","plainCitation":"[2]","noteIndex":0},"citationItems":[{"id":44,"uris":["http://zotero.org/users/local/sIEziHS8/items/VR3UIAUH"],"uri":["http://zotero.org/users/local/sIEziHS8/items/VR3UIAUH"],"itemData":{"id":44,"type":"article-journal","title":"Bacillus subtilis and its relatives: molecular biological and industrial workhorses","container-title":"Trends in biotechnology","page":"247–256","volume":"10","source":"Google Scholar","title-short":"Bacillus subtilis and its relatives","author":[{"family":"Harwood","given":"Colin R."}],"issued":{"date-parts":[["1992"]]}}}],"schema":"https://github.com/citation-style-language/schema/raw/master/csl-citation.json"} 

{"citationID":"a2k9krb4sun","properties":{"formattedCitation":"[7]","plainCitation":"[7]","noteIndex":0},"citationItems":[{"id":76,"uris":["http://zotero.org/users/local/sIEziHS8/items/KAHC7LAC"],"uri":["http://zotero.org/users/local/sIEziHS8/items/KAHC7LAC"],"itemData":{"id":76,"type":"article-journal","title":"Development of Pgrac100-based expression vectors allowing high protein production levels in Bacillus subtilis and relatively low basal expression in Escherichia coli","container-title":"Microbial Cell Factories","volume":"14","issue":"1","source":"Crossref","URL":"http://www.microbialcellfactories.com/content/14/1/72","DOI":"10.1186/s12934-015-0255-z","ISSN":"1475-2859","language":"en","author":[{"family":"Phan","given":"Trang Thi Phuong"},{"family":"Tran","given":"Linh Thuoc"},{"family":"Schumann","given":"Wolfgang"},{"family":"Nguyen","given":"Hoang Duc"}],"issued":{"date-parts":[["2015",12]]},"accessed":{"date-parts":[["2018",12,14]]}}}],"schema":"https://github.com/citation-style-language/schema/raw/master/csl-citation.json"} 

{"citationID":"a13g93on8ud","properties":{"formattedCitation":"[6]","plainCitation":"[6]","noteIndex":0},"citationItems":[{"id":75,"uris":["http://zotero.org/users/local/sIEziHS8/items/WN9Q3G27"],"uri":["http://zotero.org/users/local/sIEziHS8/items/WN9Q3G27"],"itemData":{"id":75,"type":"article-journal","title":"Construction of a 5′-controllable stabilizing element (CoSE) for over-production of heterologous proteins at high levels in Bacillus subtilis","container-title":"Journal of Biotechnology","page":"32-39","volume":"168","issue":"1","source":"Crossref","DOI":"10.1016/j.jbiotec.2013.07.031","ISSN":"01681656","language":"en","author":[{"family":"Phan","given":"Trang Thi Phuong"},{"family":"Nguyen","given":"Hoang Duc"},{"family":"Schumann","given":"Wolfgang"}],"issued":{"date-parts":[["2013",10]]}}}],"schema":"https://github.com/citation-style-language/schema/raw/master/csl-citation.json"} 

{"citationID":"ajlpfebik","properties":{"formattedCitation":"[13]","plainCitation":"[13]","noteIndex":0},"citationItems":[{"id":6,"uris":["http://zotero.org/users/local/sIEziHS8/items/IWPBWYVL"],"uri":["http://zotero.org/users/local/sIEziHS8/items/IWPBWYVL"],"itemData":{"id":6,"type":"article-journal","title":"Use of the Escherichia coli lac repressor and operator to control gene expression in Bacillus subtilis","container-title":"Proceedings of the National Academy of Sciences","page":"439-443","volume":"81","issue":"2","source":"www.pnas.org","abstract":"The Escherichia coli lac operator has been placed on the 3' side of the promoter for the penicillinase gene of Bacillus licheniformis, creating a hybrid promoter controllable by the E. coli lac repressor. The E. coli lac repressor gene has been placed under the control of a promoter and ribosome-binding site that allows expression in Bacillus subtilis. When the penicillinase gene that contains the lac operator is expressed in B. subtilis on a plasmid that also produces the lac repressor, the expression of the penicillinase gene can be modulated by isopropyl beta-D-thiogalactoside (IPTG), an inducer of the lac operon in E. coli. A similar system was constructed from a promoter of the B. subtilis phage SPO-1 and the leukocyte interferon A gene, which allowed the controlled expression of interferon in B. subtilis. These two examples show that a functional control system can be introduced into B. subtilis from E. coli.","ISSN":"0027-8424, 1091-6490","note":"PMID: 6420789","journalAbbreviation":"PNAS","language":"en","author":[{"family":"Yansura","given":"D. G."},{"family":"Henner","given":"D. J."}],"issued":{"date-parts":[["1984",1,1]]}}}],"schema":"https://github.com/citation-style-language/schema/raw/master/csl-citation.json"} 

I need to fetch title (4 titles) from each piece of json.

I tried this code

import json
import uuid

with open('1.json') as f:
    contents = json.load(f)
i = 0
while i < 25:
        i += 1
        if 'title' in contents['citationItems'][$i]['itemData']:
                print 'title    = ' + '"' + contents['citationItems'][0]['itemData']['title'] + '",'

But failed to get result Error:

  File "stack.py", line 9
    if 'title' in contents['citationItems'][$i]['itemData']:
                                            ^
SyntaxError: invalid syntax

I need result like

title    = "a"
title    = "b"
title    = "c"
title    = "d"

You have to write in pythonic way like :

if 'title' in contents['citationItems'][i]['itemData']:
   print 'title    = ' + '"' + contents['citationItems'][0]['itemData']['title'] + '",'

remove "$" from if 'title' in contents['citationItems'][$i]['itemData']: condition

EDIT:

Create json file like:

{
    "data" : [
        {"citationID":"a1u01nqrgpa","properties":{"formattedCitation":"[2]","plainCitation":"[2]",
        "noteIndex":0},"citationItems":[{"id":44,"uris":["http://zotero.org/users/local/sIEziHS8/items/VR3UIAUH"],
            "uri":["http://zotero.org/users/local/sIEziHS8/items/VR3UIAUH"],"itemData":{"id":44,"type":"article-journal",
                "title":"Bacillus subtilis and its relatives: molecular biological and industrial workhorses","container-title"
                :"Trends in biotechnology","page":"247–256","volume":"10","source":"Google Scholar","title-short":
                "Bacillus subtilis and its relatives","author":[{"family":"Harwood","given":"Colin R."}],
                "issued":{"date-parts":[["1992"]]}}}],"schema":"https://github.com/citation-style-language/schema/raw/master/csl-citation.json"} ,

{"citationID":"a2k9krb4sun","properties":{"formattedCitation":"[7]","plainCitation":"[7]","noteIndex":0},
"citationItems":[{"id":76,"uris":["http://zotero.org/users/local/sIEziHS8/items/KAHC7LAC"],"uri":["http://zotero.org/users/local/sIEziHS8/items/KAHC7LAC"],"itemData":{"id":76,"type":"article-journal","title":"Development of Pgrac100-based expression vectors allowing high protein production levels in Bacillus subtilis and relatively low basal expression in Escherichia coli","container-title":"Microbial Cell Factories","volume":"14","issue":"1","source":"Crossref","URL":"http://www.microbialcellfactories.com/content/14/1/72","DOI":"10.1186/s12934-015-0255-z","ISSN":"1475-2859","language":"en","author":[{"family":"Phan","given":"Trang Thi Phuong"},{"family":"Tran","given":"Linh Thuoc"},{"family":"Schumann","given":"Wolfgang"},{"family":"Nguyen","given":"Hoang Duc"}],"issued":{"date-parts":[["2015",12]]},"accessed":{"date-parts":[["2018",12,14]]}}}],"schema":"https://github.com/citation-style-language/schema/raw/master/csl-citation.json"} ,

{"citationID":"a13g93on8ud","properties":{"formattedCitation":"[6]","plainCitation":"[6]","noteIndex":0},
"citationItems":[{"id":75,"uris":["http://zotero.org/users/local/sIEziHS8/items/WN9Q3G27"],"uri":["http://zotero.org/users/local/sIEziHS8/items/WN9Q3G27"],"itemData":{"id":75,"type":"article-journal","title":"Construction of a 5′-controllable stabilizing element (CoSE) for over-production of heterologous proteins at high levels in Bacillus subtilis","container-title":"Journal of Biotechnology","page":"32-39","volume":"168","issue":"1","source":"Crossref","DOI":"10.1016/j.jbiotec.2013.07.031","ISSN":"01681656","language":"en","author":[{"family":"Phan","given":"Trang Thi Phuong"},{"family":"Nguyen","given":"Hoang Duc"},{"family":"Schumann","given":"Wolfgang"}],"issued":{"date-parts":[["2013",10]]}}}],"schema":"https://github.com/citation-style-language/schema/raw/master/csl-citation.json"} ,

{"citationID":"ajlpfebik","properties":{"formattedCitation":"[13]","plainCitation":"[13]","noteIndex":0},
"citationItems":[{"id":6,"uris":["http://zotero.org/users/local/sIEziHS8/items/IWPBWYVL"],
    "uri":["http://zotero.org/users/local/sIEziHS8/items/IWPBWYVL"],"itemData":{"id":6,"type":"article-journal",
        "title":"Use of the Escherichia coli lac repressor and operator to control gene expression in Bacillus subtilis","container-title":"Proceedings of the National Academy of Sciences","page":"439-443","volume":"81","issue":"2","source":"www.pnas.org","abstract":"The Escherichia coli lac operator has been placed on the 3' side of the promoter for the penicillinase gene of Bacillus licheniformis, creating a hybrid promoter controllable by the E. coli lac repressor. The E. coli lac repressor gene has been placed under the control of a promoter and ribosome-binding site that allows expression in Bacillus subtilis. When the penicillinase gene that contains the lac operator is expressed in B. subtilis on a plasmid that also produces the lac repressor, the expression of the penicillinase gene can be modulated by isopropyl beta-D-thiogalactoside (IPTG), an inducer of the lac operon in E. coli. A similar system was constructed from a promoter of the B. subtilis phage SPO-1 and the leukocyte interferon A gene, which allowed the controlled expression of interferon in B. subtilis. These two examples show that a functional control system can be introduced into B. subtilis from E. coli.","ISSN":"0027-8424, 1091-6490","note":"PMID: 6420789","journalAbbreviation":"PNAS","language":"en","author":[{"family":"Yansura","given":"D. G."},{"family":"Henner","given":"D. J."}],"issued":{"date-parts":[["1984",1,1]]}}}],"schema":"https://github.com/citation-style-language/schema/raw/master/csl-citation.json"} 
]
}

And change .py file :

import json
import uuid

with open('1.json') as f:
    contents = json.load(f)

for data in contents['data']:
    if 'title' in data['citationItems'][0]['itemData']:
        print('title    = ' + '"' + data['citationItems'][0]['itemData']['title'] + '",')

O/P

title    = "Bacillus subtilis and its relatives: molecular biological and industrial workhorses",
title    = "Development of Pgrac100-based expression vectors allowing high protein production levels in Bacillus subtilis and relatively low basal expression in Escherichia coli",
title    = "Construction of a 5′-controllable stabilizing element (CoSE) for over-production of heterologous proteins at high levels in Bacillus subtilis",
title    = "Use of the Escherichia coli lac repressor and operator to control gene expression in Bacillus subtilis",

Python tends to prefer a simpler path. We don't have $ for variables, that's a shell script thing (inherited by Perl and PHP). We generally avoid indices for iteration. And doing an extra check instead of failing is error prone, as we can see where you used $i and 0 seemingly for the same thing. Furthermore, format strings support hierarchical references.

for citationItem in contents['citationItems']:
    print('title     = "{0[itemData][title]}"'.format(citationItem))

If we might encounter a citationItem lacking a title, skipping it is easier with an exception:

for citationItem in contents['citationItems']:
    try:
        print('title     = "{0[itemData][title]}"'.format(citationItem))
    except KeyError:
        pass

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM