这是我的第一个Python程序,它通过json文件获取数据以建立科学依据:
....
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
到目前为止,它仍然可以正常工作,只是我不知道当键没有值时(即,对于一个特定的引用,json文件中没有这样的键),我不知道如何从print语句中隐藏键值。>
例如,某些科学引文中没有“公开的” ID键/值(pmd)。我不想使用两个空白值来打印“ Pubmed:”,而是要摆脱它们:
# 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:
我尝试了以下操作(如果value为空,请不要打印pmd),但是它不起作用:
print('. Pubmed: ', pbm if pbm != "")
感谢您的帮助。
答案 0 :(得分:0)
您可以执行以下操作:
js = json.loads(data)
print("Journal articles:")
for art in js['response']['docs']:
stuff = art['docType_s']
if not stuff == 'ART': continue
tit = art.get('title_s', None)[0]
nom = art.get('authFullName_s', None)[0]
jou = art.get('journalTitle_s', None)
dat = art.get('producedDateY_i', None)
pbm = art.get('pubmedId_s', None)
l = []
for e in (tit, nom, jou, dat, pbm):
if e:
if e is pbm:
l.append('Pubmed: ' + str(e))
else:
l.append(str(e))
pub = ', '.join(l).strip(', ')
print(pub)
这里使用的魔术位于dict对象提供给我们的get
函数内部。这样,您可以定义默认值,以防dict中不存在特定键(您的JSON对象只不过是Python中的dict)。使用该方法可以避免编写大量if
的情况,但仍然可以使用某种可扩展且安全的方法来构建字符串。另外,您不会两次as explained here来查询字典。
以防万一您的JSON结果将来会包含更多字段,您只需要将它们添加到此处的此字段中
...
for e in (tit, nom, jou, dat, pbm): #<-- insert more values here
...
,它们将按照您列出它们的顺序添加到字符串中。
有关内置dict函数see here的更多信息。
答案 1 :(得分:0)
您做得差不多! 除外,如果语句需要具有其他条件,那么内联
更改此
2018-07-12T09:26:35:ERROR:localstack.services.generic_proxy: Error forwarding request: HTTPConnectionPool(host='127.0.0.1', port=4564): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f68941a43d0>: Failed to establish a new connection: [Errno 111] Connection refused',)) Traceback (most recent call last):
File "/usr/local/lib/python2.7/site-packages/localstack/services/generic_proxy.py", line 201, in forward
headers=forward_headers)
File "/usr/local/lib/python2.7/site-packages/requests/api.py", line 112, in post
return request('post', url, data=data, json=json, **kwargs)
File "/usr/local/lib/python2.7/site-packages/requests/api.py", line 58, in request
return session.request(method=method, url=url, **kwargs)
File "/usr/local/lib/python2.7/site-packages/requests/sessions.py", line 508,in request
resp = self.send(prep, **send_kwargs)
File "/usr/local/lib/python2.7/site-packages/requests/sessions.py", line 618,in send
r = adapter.send(request, **kwargs)
File "/usr/local/lib/python2.7/site-packages/requests/adapters.py", line 508,in send
raise ConnectionError(e, request=request)
ConnectionError: HTTPConnectionPool(host='127.0.0.1', port=4564): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f68941a43d0>: Failed to establish a new connection: [Errno 111] Connection refused',))
至
print(nom,', ', tit, '. ', jou, '.', dat, '. Pubmed: ', pbm, sep="")
因此,如果没有发布键,则pbm将为“”,而len(pbm)为0。(也可以使用pbm =“”)。在这些情况下,不会打印“ Pubmed:'value'”。
答案 2 :(得分:-1)
我认为最好/最简洁的方法是使用“长” if/else
:
if pbm:
print(nom,', ', tit, '. ', jou, '.', dat, '. Pubmed: ', pbm, sep="")
else:
print(nom,', ', tit, '. ', jou, '.', dat, ', sep="")
您可以使用格式字符串使它更简洁:
if pbm:
print("%s, %s, %s. %s Pubmed: %s" % (nom, tit, jou, dat, pbm))
else:
print("%s, %s, %s. %s" % (nom, tit, jou, dat))
或将格式字符串与三进制... if ... else ...
组合在一起:
print("%s, %s, %s. %s%s" % (nom, tit, jou, dat, ((" Pubmed: " + pbm) if pbm else "")))
但是,就个人而言,我会选择第二种选择。