编写一个输入JSON文件的程序(格式就像 example1.json)并打印出标题字段的值。
import json
# TODO: Read your json file here and return the contents
def read_json(filename):
dt = {}
# read the file and store the contents in the variable 'dt'
with open(filename,"r") as fh:
dt = json.load(fh)
###fh = open(filename, "r")
###dt = json.load(fh)
return dt
# TODO: Pass the json file here and print the value of title field. Remove the `pass` statement
def print_title(dt):
print filename["title"]
# TODO: Input a file from the user
filename = raw_input("Enter the JSON file: ")
# The function calls are already done for you
r = read_json(filename)
print_title(r)
嗨,我是Python新手,我不确定我做错了什么。我一直收到以下消息: enter image description here
答案 0 :(得分:2)
你几乎就在那里,你只是对参数名称感到困惑。
改变这个:
def print_title(dt):
print filename["title"]
要:
def print_title(dt):
print dt["title"]