从Python中的JSON响应中提取数据并排除特定数据

时间:2018-10-13 11:22:22

标签: python json python-2.7

我的Python shell中有以下json_data:

[{u'alt_name': u'JON~1.EXT',
u'attributes': [u'DIRECTORY'],
u'create_time': 1538729344,
u'filename': u'Jon.Doe',
u'last_access_time': 1539071386,
u'last_write_time': 1539071386,
u'size': 0},

{u'attributes': [u'READONLY', u'DIRECTORY'],
u'create_time': 1536996830,
u'filename': u'Public',
u'last_access_time': 1538765229,
u'last_write_time': 1538765229,
u'size': 0},

{u'alt_name': u'ABC_IN~1',
u'attributes': [u'DIRECTORY'],
u'create_time': 1538729345,
u'filename': u'abc_inventory',
u'last_access_time': 1538729531,
u'last_write_time': 1538729531,
u'size': 0}]

我仅对“文件名”感兴趣,我想排除“公共”

目标:
乔恩·杜
abc_inventory

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

尝试一下:

json_data = [
     {u'alt_name': u'JON~1.EXT',
      u'attributes': [u'DIRECTORY'],
      u'create_time': 1538729344,
      u'filename': u'Jon.Doe',
      u'last_access_time': 1539071386,
      u'last_write_time': 1539071386,
      u'size': 0},

     {u'attributes': [u'READONLY', u'DIRECTORY'],
      u'create_time': 1536996830,
      u'filename': u'Public',
      u'last_access_time': 1538765229,
      u'last_write_time': 1538765229,
      u'size': 0},

     {u'alt_name': u'ABC_IN~1',
      u'attributes': [u'DIRECTORY'],
      u'create_time': 1538729345,
      u'filename': u'abc_inventory',
      u'last_access_time': 1538729531,
      u'last_write_time': 1538729531,
      u'size': 0}]


filenames = [i['filename'] for i in json_data]

输出将是:

['Jon.Doe', 'Public', 'abc_inventory']

如果逻辑上是不获取Public

filenames = [i['filename'] for i in json_data if i['filename'] !='Public']

输出将是:

['Jon.Doe', 'abc_inventory']