【问题标题】:python code to read and parse a string file type line by line and get the specific json key valuepython代码逐行读取和解析字符串文件类型并获取具体的json键值
【发布时间】:2021-08-18 07:54:53
【问题描述】:

我有一个字符串类型的文件,它存储来自多个页面的所有 REST API JSON 数据,格式如下

resp0 ={ "@odata.context": "https://example.com/services/api/x/odata/api/views/$metadata#vw_rpt_review_response_comment", "value": [ { "pr_comment_id": 1, "pr_comment": "Test Comment" }, { "pr_comment_id": 2, "pr_comment": "Test Comment" }, { "pr_comment_id": 3, "pr_comment": "Test Comment" } ],
"@odata.nextLink": "https://example.com/services/api/x/odata/api/views/$metadata#vw_rpt_review_response_comment?$skip=1000"
} 

resp1 ={ "@odata.context": "https://example.com/services/api/x/odata/api/views/$metadata#vw_rpt_review_response_comment", "value": [ { "pr_comment_id": 4, "pr_comment": "Test Comment" }, { "pr_comment_id": 5, "pr_comment": "Test Comment" }, { "pr_comment_id": 6, "pr_comment": "Test Comment" } ],
"@odata.nextLink": "https://example.com/services/api/x/odata/api/views/$metadata#vw_rpt_review_response_comment?$skip=2000"
}*

..so on

我希望将文件更改为

{ "value": [ { "pr_comment_id": 1, "pr_comment": "Test Comment" }, { "pr_comment_id": 2, "pr_comment": "Test Comment" }, { "pr_comment_id": 3, "pr_comment": "Test Comment" } , { "pr_comment_id": 4, "pr_comment": "Test Comment" }, { "pr_comment_id": 5, "pr_comment": "Test Comment" }, { "pr_comment_id": 6, "pr_comment": "Test Comment" } ]
}

beautify

所以我的最终目标是转换为数据帧,如下所示输入到数据库表中。

Dataframe

下面是我正在尝试的代码

with open(r'C:\Users\path\abc.json') as source:
    json_source = source.read()
    for resp0 in json_source:
        print(resp0['value'])

【问题讨论】:

  • 这真的在您的文件中吗? resp0 = 和 resp1 =?如果是这样,您应该只使用字典创建文件,这样会更容易处理

标签: python json dataframe parsing


【解决方案1】:

我制作了一个没有前面 resp0 和 resp1 的文件。这就是创建输出的方式。也许你的文件比上面的例子更干净,但这是我让它工作的唯一方法

with open('output.json') as data:
    responses = data.read().splitlines()
    value_list = []
    for resp in responses:
        if resp: # because you have blank lines between responses
            print(json.loads(resp.strip())) # needed to strip extraneous characters
            value_list.extend(json.loads(resp.strip())['value'])
my_dict = {'value': value_list}
my_dict

【讨论】:

  • 嘿@jonathan - 我故意在每个响应之前添加了几行脚本来为 resp0、resp1 加上前缀,我以为你建议过,但现在我删除了它们并执行你的脚本,现在它完美地工作了, 太感谢了! :)
猜你喜欢
  • 2014-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-13
  • 2015-10-23
  • 2018-07-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多