我有多个来自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"
答案 0 :(得分:1)
您必须以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",
答案 1 :(得分:0)
Python倾向于使用更简单的路径。我们没有 require("index/components/" + name); //fails
require("index/components/myComponent"); //work fine
作为变量,这是Shell脚本(由Perl和PHP继承)。我们通常避免迭代索引。而且进行额外的检查而不是失败很容易出错,因为我们可以看到您似乎在同一地方使用$
和$i
的地方。此外,format strings支持层次结构引用。
0
如果我们可能遇到缺少标题的citationItem,则跳过它会更容易,但有例外:
for citationItem in contents['citationItems']:
print('title = "{0[itemData][title]}"'.format(citationItem))