无论如何,在Python中有处理空响应吗?

时间:2020-08-06 08:24:49

标签: python-3.x caching empty-list

我一直在尝试从API中获取推荐详细信息,例如前5个匹配项

详细了解Colab代码

cached based API calling

输入:微笑值:O = C(N1C = CN = C1)NC2 = NC(C)= C(C3 = CC = C(S(= O)(C)= O)C(F)= C3 S2 输出 Reac = NC1 = CSC = C1C(OC)= O.O =​​ C(Cl)CCl Reag = CCN-CC.C(Cl)Cl Reac = NC1 = CSC = C1C(OC)= O.O =​​ C(Cl)CCl Reag = [] Reac = NC1 = CSC = C1C(OC)= O.O =​​ C(Cl)CCl Reag = CCN-CC.C(Cl)Cl Reac = NC1 = CSC = C1C(OC)= O.O =​​ C(Cl)CCl Reag = []

这就是价值

reac - ['O=C(OC(C)(C)C)NCCC(NC(C)(C)C)=O', 'O=[C]C(F)(F)F']
reag - ['']

在reag变量中获得空值时,它会引发错误消息

   File "C:\ProgramData\Anaconda3\lib\json\decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None

json.decoder.JSONDecodeError:预期值:第1行第1列(字符0)

我的实际逻辑在这里

from cachetools import cached
from diskcache import Cache


BASE_URL = "https://ai.chemistryinthecloud.com/smilies/"
CACHEDIR = "api_cache"

@cached(cache=Cache(CACHEDIR))
def get_similies(value):
    return requests.get(BASE_URL + value).json()

def parse_eq(eq):
    reac, _, reag = (list(v.split(".")) for v in eq.partition(">"))
    print("reac -",reac)
    print("reag -",reag)
    print(type(reag[0]))
    return {
        "reactants": {i: get_similies(i) for i in reac},
        "reagents": {i: get_similies(i) for i in reag if i is not None}
    }

result = [parse_eq(eqs) for eqs in l]

如何跳过空值并转到下一个循环值?

0 个答案:

没有答案
相关问题