gpt4 book ai didi

python - 如何从一段json文件中获取数据

转载 作者:行者123 更新时间:2023-12-01 07:52:34 25 4
gpt4 key购买 nike

我有来自 json 的多段文本例如

{"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"}

我需要从每段 json 中获取标题(4 个标题)。

我尝试了这段代码

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'] + '",'

但未能得到结果错误:

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

我需要像这样的结果

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

最佳答案

你必须以Pythonic的方式编写,例如:

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

if 'title' in contents['citationItems'][$i]['itemData']:中删除“$” 条件

编辑:

创建 json 文件,例如:

{
"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"}
]
}

并更改 .py 文件:

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 - 如何从一段json文件中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56130422/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com