我有一个.json文件,我试图转换为python字典。原始的.json文件包含如下数据:
> [
{"serial": ["Sl. No."], "name": ["Name ", "Identity No.", "Dt. of Appointment ", " Source of Recruitment ", "IntraIAS Login"], "qual": ["Qualification", "(Subject)"], "dob": ["Date of Birth", "Allotment Year", "Cadre & Domicile"], "post": ["Present Post", "With Effect From"], "rem": ["Payscale ", " Remarks"]},
{"serial": ["1"], "name": ["Ms. Naini Jayaseelan", "\u0938\u0941\u0936\u094d\u0930\u0940 \u0928\u0948\u0928\u0940 \u091c\u092f\u0938\u0940\u0932\u0928", "15/09/1980;RR"], "qual": ["M.A.(Economics)"], "dob": ["14/01/1957", "1980;UT", "Punjab"], "post": ["Secretary, Inter-state Cl Sectt, M/o Home Affairs", "04/11/2015"], "rem": ["Level 17 in the Pay Matrix", "On CD since 04/11/2015"]}
我首先将文件导入Python,如下所示:
import json
config = json.loads(open('serial.json').read())
将文件转换为python对象,如此
> [{'dob': ['Date of Birth', 'Allotment Year', 'Cadre & Domicile'],
'name': ['Name ',
'Identity No.',
'Dt. of Appointment ',
' Source of Recruitment ',
'IntraIAS Login'],
'post': ['Present Post', 'With Effect From'],
'qual': ['Qualification', '(Subject)'],
'rem': ['Payscale ', ' Remarks'],
'serial': ['Sl. No.']},
{'dob': ['14/01/1957', '1980;UT', 'Punjab'],
'name': ['Ms. Naini Jayaseelan', 'सुश्री नैनी जयसीलन', '15/09/1980;RR'],
'post': ['Secretary, Inter-state Cl Sectt, M/o Home Affairs', '04/11/2015'],
'qual': ['M.A.(Economics)'],
'rem': ['Level 17 in the Pay Matrix', 'On CD since 04/11/2015'],
'serial': ['1']},
虽然它看起来像是一本字典,但Python告诉我它是一个列表
[In 59] type(config)
[Out 59] list
我无法理解这一部分。
问题:如何成功将其转换为python dict,如何从中提取单个元素?
答案 0 :(得分:0)
原始json不是字典,所以python不会创建字典。它实际上是一个字典列表,这是你在示例输出中显示的。
大括号:{}
表示字典,方括号:[]
表示列表。在您的示例中,您使用逗号围绕两组花括号的方括号,这是字典列表的标准语法。