Python从JSON变量中提取值-TypeError:字符串索引必须为整数

时间:2019-01-23 09:15:24

标签: python json

我从this SO link之后的以下代码中获得了字典

,并且需要获取字典中每个键的应用程序名称,源和消息,因此我尝试将其传输到JSON文件中

 if mail["Subject"].find("Alert for  Clarion prod errors") > 0 :
          body = get_autosys_body(mail)
          info = {}
          segments = body.split(' ')
          for line in body.splitlines():
            if 'Application name' and 'null' in line:
                 info['test'] = segments[0] + ' ' + segments[1] + ' ' + segments[2]  + ' ' + segments[3] + ' ' + segments[4]
            elif 'Application name' in line:
                 info['test'] = segments[0] + ' ' + segments[1] + ' ' + segments[2]  + ' ' + segments[3] + ' ' + segments[4] + ' ' + segments[5] + segments[6] + ' ' + segments[7] +  ' ' + segments[8] + ' ' + segments[9]
            r = json.dumps(info['test'])
            loaded_r = json.loads(r)
            print(str(r['Source']))

我有这本字典

print(info ['test'])

Application name: Clarion.MarketSnapService
Source: p2prog15
Timestamp: 2019-01-22T00:00:43.901Z
Message:

Application name: Clarion.API
Source: adc266f53205
Timestamp: 2019-01-23T07:42:12.649Z
Message: HTTP"GET" "/api/fx/pricer2/groups" responded 500

我没有错误地将其转换为JSON

r = json.dumps(info['test'])
          loaded_r = json.loads(r)

,并尝试从中提取Application_name时:

loaded_r['Application name']

或来源

loaded_r['Source']

我得到TypeError: string indices must be integers

如重复链接所建议,也尝试了print (loaded_r['Source'][0])print(str(r['Source']))但相同的

邮件正文示例(用于只保留前几行以删除重复项的段):

Source: p2prog15
Timestamp: 2019-01-22T00:00:43.901Z

Message: null



For instructions please see: https://protect-eu.mimecast.com/s/mvuLCN94BiKwOlsmSF0i?domain=wiki


Application name: Clarion.API
Source: adc266f53205
Timestamp: 2019-01-23T07:42:12.649Z
Message: HTTP "GET" "/api/fx/pricer2/groups" responded 500 in 7795.6441 ms TraceId "20b810bb06dce097"


Application name: Clarion.API
Source: adc266f53205
Timestamp: 2019-01-23T07:42:12.646Z
Message: Unhandled exception



For instructions please see: https://protect-eu.mimecast.com/s/JgX4CoQLlHNEPKu1czKG?domain=wiki

字典存储在info变量中

{'test': '\r\nApplication name: Clarion.MarketSnapService\r\nSource: p2prog15\r\nTimestamp: 2019-01-22T00:00:43.901Z\r\nMessage:'}
{'test': '\r\nApplication name: Clarion.API\r\nSource: adc266f53205\r\nTimestamp: 2019-01-23T07:42:12.649Z\r\nMessage: HTTP"GET" "/api/fx/pricer2/groups" responded 500'}

2 个答案:

答案 0 :(得分:0)

我认为loaded_r是字符串而不是字典。

答案 1 :(得分:0)

我认为我现在很好,做了一些“巨型游戏”,但是它可以工作,将字典转换为字符串,然后使用正则表达式。谢谢大家

res = ','.join([','.join(i) for i in info.items()])
          x = res.replace('test,','')

          regex1=r'Application name:\s*(.+?)\s+Source'
          regex2=r'Source:\s*(.+?)\s+Timestamp:'
          regex3 = r'(?<!^)Message:\s*.*'
          a = re.findall(regex1 ,x)
          b = re.findall(regex2 ,x)
          c = re.findall(regex3, x)
          print (a, b, c)