Python 3.5.1:NameError:名称“json”未定义

2024-03-19

#!/usr/bin/env python
# encoding: utf-8

import tweepy #https://github.com/tweepy/tweepy
import json as simplejson


    #write tweet objects to JSON
    file = open('tweet.json', 'wb') 
    print ("Writing tweet objects to JSON please wait...")
    for status in alltweets:
        json.dump(status._json,file,sort_keys = True,indent = 4)

    #close the file
    print ("Done")
    file.close()

if __name__ == '__main__':
    #pass in the username of the account you want to download
    get_all_tweets("@AlertZaAfrica")

python 编译器说第 54 行是错误的。我已经将 import json 定义为 simplejson。我定义 import json 的区域如上所示。


您应该首先将 simplejson 安装到您的系统中以便能够导入它:

$ sudo pip3 install simplejson

然后在您的代码中您现在可以导入它:

import simplejson as json

从现在开始,您将能够使用以下方式访问 simplejson 包json.

请注意,如果您仍在使用 python 2 - 尽管这不是OPs最初的问题 - 你应该做一个

$ sudo pip install simplejson

instead.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Python 3.5.1:NameError:名称“json”未定义 的相关文章

随机推荐