Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中加载JSON_Python_Json - Fatal编程技术网

在python中加载JSON

在python中加载JSON,python,json,Python,Json,我在python中加载JSON时遇到问题。我正在使用Python2.7,我有一个JSON文件要加载。我做到了: movies = json.load(open(FBO_REF_FILE, 'r')) 但当我展示它时,我得到了一份充满: {u'id_yeyecine': 42753, u'budget_dollars': u'85', u'classification': u'Tous publics', u'pays': u'US', u'budget_euros': u'0', u'dpw_

我在python中加载JSON时遇到问题。我正在使用Python2.7,我有一个JSON文件要加载。我做到了:

movies = json.load(open(FBO_REF_FILE, 'r'))
但当我展示它时,我得到了一份充满:

{u'id_yeyecine': 42753, u'budget_dollars': u'85', u'classification': u'Tous publics', u'pays': u'US', u'budget_euros': u'0', u'dpw_entrees_fr': 132326, u'realisateurs': u'Brad Peyton, Kevin Lima', u'is_art_et_essai': u'NON', u'distributeur_video': u'Warner hv', u'genre_gfk_1': u'ENFANT', u'genre_gfk_2': u'FILM FAMILLE', u'genre_gfk_3': u'FILM FAMILLE', u'is_3D': u'OUI', u'fid': 16429, u'cum_entrees_pp': 58076, u'titre': u'COMME CHIENS ET CHATS LA REVANCHE DE KITTY GALORE', u'psp_entrees': 963, u'cum_entrees_fr': 348225, u'dps_copies_fr': 453, u'dpj_entrees_pp': 7436, u'visa': 127021, u'dps_entrees_fr': 178908, u'genre': u'Com\xe9die', u'distributeur': u'WARNER BROS.', u'editeur_video': u'Warner bros', u'psp_copies': 15, u'dpw_entrees_pp': 26195, u'id_imdb': None, u'date_sortie_video': u'2010-12-06', u'dps_copies_pp': 39, u'date_sortie': u'2010-08-04', u'dps_entrees_pp': 32913, u'dpj_entrees_fr': 40369, u'ecrivains': u'', u'acteurs': u"Chris O'donnell, Jack McBrayer", u'is_premier_film': u'NON'}
我尝试使用ast,但出现以下错误:字符串格式错误。使用last时出现的错误如下:

    153     if cursor is None:
    154         movies = json.load(open(FBO_REF_FILE, 'r'))
--> 155         movies = ast.literal_eval(movies)
    156         for movie in movies:
    157             if movies[movie]['id_allocine'] == allocine_id:

/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ast.pyc in literal_eval(node_or_string)
     78                 return left - right
     79         raise ValueError('malformed string')
---> 80     return _convert(node_or_string)
     81 
     82 

/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ast.pyc in _convert(node)
     77             else:
     78                 return left - right
---> 79         raise ValueError('malformed string')
     80     return _convert(node_or_string)
     81 

ValueError: malformed string
content = None
with open(FBO_REF_FILE, 'r') as f:
    content = json.loads(f.read())
print content  # content is a dict containing the parsed json data
print content['id_yeyecine']
print content['budget_dollars']

如果您想漂亮地打印您的词典:

json.dumps(movies, sort_keys=True, indent=4)

或者将pprint:

json一起使用。在你的情况下,这是一个命令

使用
open
加载文件

如果不想解析json文件,只需执行以下操作

content = None
with open(FBO_REF_FILE, 'r') as f:
    content = f.read()
print content  # content is a string contaning the content of the file
如果要将json文件解析为python的数据类型,请执行以下操作:

    153     if cursor is None:
    154         movies = json.load(open(FBO_REF_FILE, 'r'))
--> 155         movies = ast.literal_eval(movies)
    156         for movie in movies:
    157             if movies[movie]['id_allocine'] == allocine_id:

/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ast.pyc in literal_eval(node_or_string)
     78                 return left - right
     79         raise ValueError('malformed string')
---> 80     return _convert(node_or_string)
     81 
     82 

/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ast.pyc in _convert(node)
     77             else:
     78                 return left - right
---> 79         raise ValueError('malformed string')
     80     return _convert(node_or_string)
     81 

ValueError: malformed string
content = None
with open(FBO_REF_FILE, 'r') as f:
    content = json.loads(f.read())
print content  # content is a dict containing the parsed json data
print content['id_yeyecine']
print content['budget_dollars']

要阅读
电影
,请使用常规dict方法:

id_yeyecine = movies["id_yeyecine"] 

现在
id\u yeyecine
42753

您如何“显示”
电影
?我觉得这很好:JSON按预期被解析为dict。你能告诉我们文件的内容吗???也许你得到了正确的答案result@LutzHorn我正在使用print来显示它,错误发生的确切时间是什么时候?你能提供完整的回溯和呼叫代码吗?我不认为有什么问题。