美文网首页
Python 列表生成式解析json

Python 列表生成式解析json

作者: 哆啦A梦没有毛 | 来源:发表于2018-08-29 11:50 被阅读35次

    一段有道翻译返回结果的json数据如下(翻译的是《Game of Thrones》):

    {
      "translateResult": [
        [
          {
            "tgt": "我们应该往回走,”盖瑞催促着,周围的树林开始变暗。",
            "src": "We should start back,” Gared urged as the woods began to grow dark around them. "
          },
          {
            "tgt": "“野人”已经死了。",
            "src": "“The wildlings are dead.”"
          }
        ],
        [
          {
            "tgt": "“死人吓着你了吗?”",
            "src": "“Do the dead frighten you?"
          },
          {
            "tgt": "威玛·罗伊斯爵士微笑着问道。",
            "src": "” Ser Waymar Royce asked with just the hint of a smile."
          }
        ],
        [
          {
            "tgt": "盖瑞没有上钩。",
            "src": "Gared did not rise to the bait. "
          },
          {
            "tgt": "他是个五十多岁的老人,他曾见过贵族们来来去去。",
            "src": "He was an old man, past fifty, and he had seen the lordlings come and go. "
          },
          {
            "tgt": "“死就是死,”他说。",
            "src": "“Dead is dead,” he said. "
          },
          {
            "tgt": "“我们和死人没有关系。”",
            "src": "“We have no business with the dead.”"
          }
        ],
        [
          {
            "tgt": "“他们死了吗?",
            "src": "“Are they dead?"
          },
          {
            "tgt": "”罗伊斯轻声问道。",
            "src": "” Royce asked softly. "
          },
          {
            "tgt": "“我们证明什么?”",
            "src": "“What proof have we?”"
          }
        ]
      ],
      "errorCode": 0,
      "type": "en2zh-CHS"
    }
    
    

    我是整段翻译,需要将所有翻译结果整合成一个字符串,但是这个json稍微有些复杂,数组里面套数组。

    来把它一层一层的拆开,先拆第一层:

    import json
    
    if __name__ == '__main__':
        json_str = None
        with open('json.json', encoding='utf-8') as f:
            json_str = f.read()
        json = json.loads(json_str)
        for items in json['translateResult']:
            print(items)
    
    # 输出结果:
    [{'tgt': '我们应该往回走,”盖瑞催促着,周围的树林开始变暗。', 'src': 'We should start back,” Gared urged as the woods began to grow dark around them. '}, {'tgt': '“野人”已经死了。', 'src': '“The wildlings are dead.”'}]
    [{'tgt': '“死人吓着你了吗?”', 'src': '“Do the dead frighten you?'}, {'tgt': '威玛·罗伊斯爵士微笑着问道。', 'src': '” Ser Waymar Royce asked with just the hint of a smile.'}]
    [{'tgt': '盖瑞没有上钩。', 'src': 'Gared did not rise to the bait. '}, {'tgt': '他是个五十多岁的老人,他曾见过贵族们来来去去。', 'src': 'He was an old man, past fifty, and he had seen the lordlings come and go. '}, {'tgt': '“死就是死,”他说。', 'src': '“Dead is dead,” he said. '}, {'tgt': '“我们和死人没有关系。”', 'src': '“We have no business with the dead.”'}]
    [{'tgt': '“他们死了吗?', 'src': '“Are they dead?'}, {'tgt': '”罗伊斯轻声问道。', 'src': '” Royce asked softly. '}, {'tgt': '“我们证明什么?”', 'src': '“What proof have we?”'}]
    

    每个数组里里面又有不止一条数据,再来个循环,把数组中的数据也提取出来:

        for items in json['translateResult']:
            for item in items:
                print(item['tgt'])
    # 输出结果:
    我们应该往回走,”盖瑞催促着,周围的树林开始变暗。
    “野人”已经死了。
    “死人吓着你了吗?”
    威玛·罗伊斯爵士微笑着问道。
    盖瑞没有上钩。
    他是个五十多岁的老人,他曾见过贵族们来来去去。
    “死就是死,”他说。
    “我们和死人没有关系。”
    “他们死了吗?
    ”罗伊斯轻声问道。
    “我们证明什么?”
    

    已经OK了,现在用列表生成式一步搞定吧(我刚知道列表生成式可以用双重循环):

        trans_list=[item['tgt'] for items in json['translateResult'] for item in items]
        print(trans_list)
    # 输出结果:
    ['我们应该往回走,”盖瑞催促着,周围的树林开始变暗。', '“野人”已经死了。', '“死人吓着你了吗?”', '威玛·罗伊斯爵士微笑着问道。', '盖瑞没有上钩。', '他是个五十多岁的老人,他曾见过贵族们来来去去。', '“死就是死,”他说。', '“我们和死人没有关系。”', '“他们死了吗?', '”罗伊斯轻声问道。', '“我们证明什么?”']
    

    再把list合并成一个字符串吧:

        trans_str = ''.join(trans_list)
        print(trans_str)
    # 输出结果:
    我们应该往回走,”盖瑞催促着,周围的树林开始变暗。“野人”已经死了。“死人吓着你了吗?”威玛·罗伊斯爵士微笑着问道。盖瑞没有上钩。他是个五十多岁的老人,他曾见过贵族们来来去去。“死就是死,”他说。“我们和死人没有关系。”“他们死了吗?”罗伊斯轻声问道。“我们证明什么?”
    

    最终代码:

    import json
    
    if __name__ == '__main__':
        json_str = None
        with open('json.json', encoding='utf-8') as f:
            json_str = f.read()
        json = json.loads(json_str)
        # for items in json['translateResult']:
        #     for item in items:
        #         print(item['tgt'])
        trans_list = [item['tgt'] for items in json['translateResult'] for item in items]
        # print(trans_list)
        trans_str = ''.join(trans_list)
        print(trans_str)
    

    相关文章

      网友评论

          本文标题:Python 列表生成式解析json

          本文链接:https://www.haomeiwen.com/subject/hkphwftx.html