将JSON数据导入Python [英] Import JSON data into Python

查看:220
本文介绍了将JSON数据导入Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图找到一种将JSON文件中的数据导入Python的简单方法.我最初的想法是逐行读取文件,但这可能意味着应该在库中进行一些额外的处理.

Trying to find a simple way to import data from a JSON file into Python. My initial thoughts would be to read the file line by line, but this might imply some additional processing which should already be done in a library.

理想的解决方案如下所示:

The ideal solution would look something like:

import json_library

the_data = json_library.load_from_file('my_file.json')

其中"my_file.json"包含JSON格式的变量.

where 'my_file.json' contains a JSON-formatted variable.

推荐答案

json 会为您做到这一点.

import json
data = json.load(open('my_file.json', 'r'))

演示文件的内容:

{"hello":"stack overflow"}

演示:

>>> import json
>>> print(json.load(open('my_file.json','r')))
{u'hello': u'stack overflow'}

这篇关于将JSON数据导入Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆