Python 列表生成式解析json

一段有道翻译返回结果的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)
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明AGI阅读 16,018评论 3 119
  • 她,是一个普通的不能再普通的女人,却常常吸引着我的注意力; 她,是大楼里众多清洁工中的一名,负责我们这一层的打扫;...
    怿溦阅读 243评论 0 0
  • 美国戴尔电脑公司总裁麦克·戴尔曾说:"创业没有准则。"那么就创业而言,也没有哪一种固定的模式可以保证我们一劳永逸地...
    涛新一步阅读 207评论 0 0
  • 4月16日 祈祷: 美好的早晨,请引领着我!今天无论我做什么都会做的很好!爱拥抱着我,环绕着我,我的内心平安坦荡!...
    闪光的种子阅读 120评论 0 1