【问题标题】:How to read a json file and return as dictionary in Python如何在 Python 中读取 json 文件并作为字典返回
【发布时间】:2017-05-19 12:00:51
【问题描述】:

试图读取一个 json 文件并作为字典返回:

  def js_r(filename):
  with open('num.json', 'r')as f_in:
  json_d = f_read()

dict函数如何返回?

【问题讨论】:

    标签: json python-2.7


    【解决方案1】:

    使用json 模块对其进行解码。

    import json
    
    def js_r(filename: str):
        with open(filename) as f_in:
            return json.load(f_in)
    
    if __name__ == "__main__":
        my_data = js_r('num.json')
        print(my_data)
    

    【讨论】:

    • 你调用了这个函数吗?我已经更新了示例。
    • 谢谢 :),我注意到我这边有一些错字.. 它现在可以工作了
    • 但是它返回的对象是list,而不是dict,怎么转换呢?
    猜你喜欢
    • 2017-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-19
    • 2017-03-22
    • 1970-01-01
    • 2013-03-02
    相关资源
    最近更新 更多