gpt4 book ai didi

python 3.5.1 : NameError: name 'json' is not defined

转载 作者:太空狗 更新时间:2023-10-29 22:07:33 29 4
gpt4 key购买 nike

#!/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

从现在开始,您将能够使用 json 访问 simplejson 包。

请注意,如果您仍在使用 python 2 - 尽管这不是 OP 的原始问题 - 您应该做一个

$ sudo pip install simplejson

相反。

关于 python 3.5.1 : NameError: name 'json' is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41001973/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com