我正在调用以文本/ html格式发送响应的第三方API。这是一个键值对,但不完全是json。
current:{ data:"true" ,results:15}
预期:"data":"true" , "results":15}
我正在尝试使用json.loads
进行解析以创建字典,但是从结构上可以明显看出它并不是json,而python也会引发错误。
{JSONDecodeError}期望属性名称用双引号引起来:第1行第3列(字符2)
有没有办法修复响应并将其转换为正确的json,还是有其他方法可以从响应中获取键值对
在这种情况下,yaml无法帮助我,整个数据:
{ success:true ,results:9,rows:[{sym:"FEDERALBNK",ser:"EQ",Ind:"-",face:"2",sub:"Annual General Meeting And Dividend Rs.5/- Per Share",exDt:"02-Sep-2010",recordDt:"-",bcStartDt:"04-Sep-2010",bcEndDt:"13-Sep-2010",ndStartDt:"-",comp:"The Federal Bank Limited",isin:"INE171A01011",ndEndDt:"-"},{sym:"FEDERALBNK",ser:"EQ",Ind:"-",face:"2",sub:"Annual General Meeting And Dividend Rs.8.50 Per Share",exDt:"23-Aug-2011",recordDt:"-",bcStartDt:"25-Aug-2011",bcEndDt:"03-Sep-2011",ndStartDt:"-",comp:"The Federal Bank Limited",isin:"INE171A01011",ndEndDt:"-"},{sym:"FEDERALBNK",ser:"EQ",Ind:"-",face:"2",sub:"Annual General Meeting And Dividend Rs.9/- Per Share",exDt:"20-Jul-2012",recordDt:"-",bcStartDt:"24-Jul-2012",bcEndDt:"02-Aug-2012",ndStartDt:"-",comp:"The Federal Bank Limited",isin:"INE171A01011",ndEndDt:"-"},{sym:"FEDERALBNK",ser:"EQ",Ind:"-",face:"2",sub:"Annual General Meeting/Dividend Rs 9/- Per Share",exDt:"09-Jul-2013",recordDt:"-",bcStartDt:"11-Jul-2013",bcEndDt:"20-Jul-2013",ndStartDt:"-",comp:"The Federal Bank Limited",isin:"INE171A01011",ndEndDt:"-"},{sym:"FEDERALBNK",ser:"EQ",Ind:"-",face:"2",sub:"Annual General Meeting / Dividend - Rs 2/- Per Share",exDt:"04-Jul-2014",recordDt:"-",bcStartDt:"08-Jul-2014",bcEndDt:"17-Jul-2014",ndStartDt:"-",comp:"The Federal Bank Limited",isin:"INE171A01011",ndEndDt:"-"},{sym:"FEDERALBNK",ser:"EQ",Ind:"-",face:"2",sub:"Annual General Meeting / Dividend - Rs 2.20/- Per Share",exDt:"28-May-2015",recordDt:"-",bcStartDt:"01-Jun-2015",bcEndDt:"05-Jun-2015",ndStartDt:"-",comp:"The Federal Bank Limited",isin:"INE171A01011",ndEndDt:"-"},{sym:"FEDERALBNK",ser:"EQ",Ind:"-",face:"2",sub:" Annual General Meeting/ Dividend -Re 0.70 Per Share",exDt:"29-Jul-2016",recordDt:"-",bcStartDt:"02-Aug-2016",bcEndDt:"11-Aug-2016",ndStartDt:"-",comp:"The Federal Bank Limited",isin:"INE171A01011",ndEndDt:"-"},{sym:"FEDERALBNK",ser:"EQ",Ind:"-",face:"2",sub:" Annual General Meeting/Dividend - Re 0.90 Per Share",exDt:"03-Jul-2017",recordDt:"-",bcStartDt:"05-Jul-2017",bcEndDt:"14-Jul-2017",ndStartDt:"-",comp:"The Federal Bank Limited",isin:"INE171A01011",ndEndDt:"-"},{sym:"FEDERALBNK",ser:"EQ",Ind:"-",face:"2",sub:" Annual General Meeting/Dividend- Re 1 Per Share",exDt:"02-Aug-2018",recordDt:"-",bcStartDt:"04-Aug-2018",bcEndDt:"10-Aug-2018",ndStartDt:"-",comp:"The Federal Bank Limited",isin:"INE171A01011",ndEndDt:"-"}]}
使用yaml后会变成:
{'success:true': None, 'results:9': None, 'rows': [{'sym:"FEDERALBNK"': None, 'ser:"EQ"': None, 'Ind:"-"': None, 'face:"2"': None, 'sub:"Annual General Meeting And Dividend Rs.5/- Per Share"': None, 'exDt:"02-Sep-2010"': None, 'recordDt:"-"': None, 'bcStartDt:"04-Sep-2010"': None, 'bcEndDt:"13-Sep-2010"': None, 'ndStartDt:"-"': None, 'comp:"The Federal Bank Limited"': None, 'isin:"INE171A01011"': None, 'ndEndDt:"-"': None}, {'sym:"FEDERALBNK"': None, 'ser:"EQ"': None, 'Ind:"-"': None, 'face:"2"': None, 'sub:"Annual General Meeting And Dividend Rs.8.50 Per Share"': None, 'exDt:"23-Aug-2011"': None, 'recordDt:"-"': None, 'bcStartDt:"25-Aug-2011"': None, 'bcEndDt:"03-Sep-2011"': None, 'ndStartDt:"-"': None, 'comp:"The Federal Bank Limited"': None, 'isin:"INE171A01011"': None, 'ndEndDt:"-"': None}, {'sym:"FEDERALBNK"': None, 'ser:"EQ"': None, 'Ind:"-"': None, 'face:"2"': None, 'sub:"Annual General Meeting And Dividend Rs.9/- Per Share"': None, 'exDt:"20-Jul-2012"': None, 'recordDt:"-"': None, 'bcStartDt:"24-Jul-2012"': None, 'bcEndDt:"02-Aug-2012"': None, 'ndStartDt:"-"': None, 'comp:"The Federal Bank Limited"': None, 'isin:"INE171A01011"': None, 'ndEndDt:"-"': None}, {'sym:"FEDERALBNK"': None, 'ser:"EQ"': None, 'Ind:"-"': None, 'face:"2"': None, 'sub:"Annual General Meeting/Dividend Rs 9/- Per Share"': None, 'exDt:"09-Jul-2013"': None, 'recordDt:"-"': None, 'bcStartDt:"11-Jul-2013"': None, 'bcEndDt:"20-Jul-2013"': None, 'ndStartDt:"-"': None, 'comp:"The Federal Bank Limited"': None, 'isin:"INE171A01011"': None, 'ndEndDt:"-"': None}, {'sym:"FEDERALBNK"': None, 'ser:"EQ"': None, 'Ind:"-"': None, 'face:"2"': None, 'sub:"Annual General Meeting / Dividend - Rs 2/- Per Share"': None, 'exDt:"04-Jul-2014"': None, 'recordDt:"-"': None, 'bcStartDt:"08-Jul-2014"': None, 'bcEndDt:"17-Jul-2014"': None, 'ndStartDt:"-"': None, 'comp:"The Federal Bank Limited"': None, 'isin:"INE171A01011"': None, 'ndEndDt:"-"': None}, {'sym:"FEDERALBNK"': None, 'ser:"EQ"': None, 'Ind:"-"': None, 'face:"2"': None, 'sub:"Annual General Meeting / Dividend - Rs 2.20/- Per Share"': None, 'exDt:"28-May-2015"': None, 'recordDt:"-"': None, 'bcStartDt:"01-Jun-2015"': None, 'bcEndDt:"05-Jun-2015"': None, 'ndStartDt:"-"': None, 'comp:"The Federal Bank Limited"': None, 'isin:"INE171A01011"': None, 'ndEndDt:"-"': None}, {'sym:"FEDERALBNK"': None, 'ser:"EQ"': None, 'Ind:"-"': None, 'face:"2"': None, 'sub:" Annual General Meeting/ Dividend -Re 0.70 Per Share"': None, 'exDt:"29-Jul-2016"': None, 'recordDt:"-"': None, 'bcStartDt:"02-Aug-2016"': None, 'bcEndDt:"11-Aug-2016"': None, 'ndStartDt:"-"': None, 'comp:"The Federal Bank Limited"': None, 'isin:"INE171A01011"': None, 'ndEndDt:"-"': None}, {'sym:"FEDERALBNK"': None, 'ser:"EQ"': None, 'Ind:"-"': None, 'face:"2"': None, 'sub:" Annual General Meeting/Dividend - Re 0.90 Per Share"': None, 'exDt:"03-Jul-2017"': None, 'recordDt:"-"': None, 'bcStartDt:"05-Jul-2017"': None, 'bcEndDt:"14-Jul-2017"': None, 'ndStartDt:"-"': None, 'comp:"The Federal Bank Limited"': None, 'isin:"INE171A01011"': None, 'ndEndDt:"-"': None}, {'sym:"FEDERALBNK"': None, 'ser:"EQ"': None, 'Ind:"-"': None, 'face:"2"': None, 'sub:" Annual General Meeting/Dividend- Re 1 Per Share"': None, 'exDt:"02-Aug-2018"': None, 'recordDt:"-"': None, 'bcStartDt:"04-Aug-2018"': None, 'bcEndDt:"10-Aug-2018"': None, 'ndStartDt:"-"': None, 'comp:"The Federal Bank Limited"': None, 'isin:"INE171A01011"': None, 'ndEndDt:"-"': None}]}
我无法访问,例如:
yaml.load(corporate_action)['rows'][0]['sym']
答案 0 :(得分:2)
正则表达式在这里可能会有所帮助。您可以使用re.sub
例如:
import re
import json
data = '{ data:"true" ,results:15}'
data = json.loads(re.sub(r"(\w+):", r'"\1":', data))
print(data)
输出:
{u'data': u'true', u'results': 15}