我有一個JSON文件那樣:如何從python中的JSON文件中選擇數據?
[
{
"course": "CMPT 102 D1",
"instructor": "hamarneh",
"students": [
"axc5",
"csf10",
"ctu1",
"nmw15",
"nsm12",
"ppy1",
"qtg13",
"tim1",
"tkd10",
"vhm8",
"vsv1",
"wps1",
"xup12",
"yqt6"
],
"title": "Scientific Cmpt.Prgm"
}]
,這裏是我的代碼在Python:
import json
json_data=open('jsonfile')
data=json.load(json_data)
print(data['students'])
,但它顯示了一個錯誤:
print(data['students'])
TypeError: list indices must be integers, not str
請幫助!
而另一個問題: 假設JSON文件包含許多類似上述結構的課程。我怎麼可以這樣做:
Select students, count(course) as course_number from tblstudent
group by students
打印「數據」並查看它的外觀。我敢打賭,這是一個口號清單,不是一個字典。 – sashkello