【问题标题】:json API data extractionjson API 数据提取
【发布时间】:2017-03-15 18:11:37
【问题描述】:

我无法从 json API 中提取数据。我正在尝试获取“Total_allele_count”。我能够从 API 中提取其他数据,但是当涉及到“exac”数据时它不起作用。

 "alterations" : [
      {
         "Gene_position" : "3164,,,",
         "exac" : {
            "close_matches" : [],
            "exact_matches" : [
               {
                  "exac_allele" : [
                     {
                        "West_allele_count" : 0,
                        "Total_allele_count" : "52413,1",
                        "Male_allele_count" : "11142,0"

这就是失败的地方。我是不是在看东西?

row = alter(data, 'exac', 'Total_allele_count', row)

我的方法

def alter(source, org, allele, fileRow):
    try:
        toAppend = [int(x) for x in source['alterations'][0][org]['exact_matches'][0][allele].split('/')]
        #fileRow.append(str(len(toAppend)))
        fileRow.append(toAppend[1]/sum(toAppend))
    except:
        fileRow.append('N/A')
    return fileRow    

【问题讨论】:

  • 你的json不完整。
  • “不工作”定义不够明确,无法提供分析。当你说“这就是失败的地方”时......你确定吗?你怎么知道的?我怀疑裸露的except: 隐藏了一些有趣的回溯,可用于发现问题所在......

标签: python json api extract


【解决方案1】:

诀窍是编写一个执行错误报告的查找函数,以便您知道自己没有错过任何关卡。比如:

def lookup(json, *path):
    if not path:
        return json

    first = path[0]
    rest = path[1:]

    try:
        sub_json = json[first]
    except (TypeError, LookupError) as e:
        raise ValueError("Failed to look up %r in %s" % (first, json))
    return lookup(sub_json, *rest)

那么您可以将alter 实现为:

def alter(source, org, allele, fileRow):
    value = lookup(source, 'alterations', 0, org, 'exact_matches', 0, allele)
    try:
        toAppend = value.split('/')
        fileRow.append(toAppend[1]/sum(toAppend))
    except:
        fileRow.append('N/A')
    return fileRow    

以上内容,并关闭您的数据:

data = { "alterations" : [
      {
         "Gene_position" : "3164,,,",
         "exac" : {
            "close_matches" : [],
            "exact_matches" : [
               {
                  "exac_allele" : [
                     {
                        "West_allele_count" : 0,
                        "Total_allele_count" : "52413,1",
                        "Male_allele_count" : "11142,0"
                        }]}]}}]}

我们可以拨打alter:

alter(data, 'exac', 'Total_allele_count', '')

并获得以下回溯:

Traceback (most recent call last):
  File "<stdin>", line 35, in <module>
  File "<stdin>", line 32, in alter
  File "<stdin>", line 27, in lookup
  File "<stdin>", line 27, in lookup
  File "<stdin>", line 27, in lookup
  File "<stdin>", line 27, in lookup
  File "<stdin>", line 27, in lookup
  File "<stdin>", line 26, in lookup
ValueError: Failed to look up 'Total_allele_count' in {'exac_allele': [{'Total_allele_count': '52413,1', 'Male_allele_count': '11142,0', 'West_allele_count': 0}]}

这告诉我们

lookup(source, 'alterations', 0, org, 'exact_matches', 0, allele)

在最后一个之前缺少一个级别,应该是:

lookup(source, 'alterations', 0, org, 'exact_matches', 0, 'exac_allele', allele)

运行它,我们得到以下异常:

ValueError: Failed to look up 'Total_allele_count' in [{'Total_allele_count': '52413,1', 'Male_allele_count': '11142,0', 'West_allele_count': 0}]

如果你仔细看,value是一个list,key是一个string,所以最终的解决方案是:

lookup(source, 'alterations', 0, org, 'exact_matches', 0, 'exac_allele', 0, allele)

【讨论】:

    【解决方案2】:

    怎么样:

    toAppend = [int(x) for x in source['alterations'][0][org]['exact_matches'][0]['exac_allele'][0][allele].split('/')]

    (我猜这只是一个直接的缺陷/错误,并没有真正误解 json 结构)

    您还应该删除裸露的 except(正如@thebjorn 在评论中所暗示的那样),因为它可能掩盖了这个错误(可能还有其他错误)。

    【讨论】:

      猜你喜欢
      • 2013-09-25
      • 2018-04-15
      • 2023-02-25
      • 2021-11-24
      • 2017-04-16
      • 2013-01-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多