我在ubuntu服务器上运行tweepy代码。我的代码在本地机器上运行良好,当我把它放在ubuntu服务器上时代码根本不起作用。我试图自己创建输出文件,但代码将无法运行。
这是代码:
action
错误异常是:
import tweepy
import time
import json
import csv
import sys
from tweepy import OAuthHandler
from tweepy import Stream
from tweepy.streaming import StreamListener
consumer_key = '85gfoc9I'
consumer_secret = 'Cw'
access_token = '61'
access_secret = 'd4'
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
api = tweepy.API(auth)
class Listener(StreamListener):
def on_data(self, data):
try:
jsonData = json.loads(data)
createdAt = jsonData['created_at']
userId = jsonData['id_str']
userName = jsonData['user']['name']
status = jsonData['user']['statuses_count']
temen = jsonData['user']['friends_count']
follower = jsonData['user']['followers_count']
Namatempat = jsonData['place']['name']
geo = jsonData['place']['bounding_box']['coordinates']
text = jsonData['text']
saveThis = createdAt +" :: " + userId +" :: " + userName+ " :: " + str(status) + " :: " + str(temen) + " :: " + str(follower) + " :: " + str(Namatempat) + " :: " +str(geo)+" :: " + text
saveFile = open('tescsv.csv','a', encoding="utf-8")
saveFile.write(saveThis)
saveThis.encode('utf-8')
saveFile.write('\n')
except BaseException as e:
print("Error on_data: %s" % str(e))
return True
def on_error(self, status):
print(status)
return True
我的尝试:
我的猜测是,编码一定有问题,我需要在ubuntu服务器上安装一些东西,这将与csv有关。希望你能帮助我们。
答案 0 :(得分:0)
open
函数没有关键字参数encoding
,因此您的错误为'encoding' is an invalid keyword argument for this function
。简单的做法:
saveFile = open('tescsv.csv', 'a')
答案 1 :(得分:0)
从open()
的文档中,open (name[, mode[, buffering]])
所需的缓冲区大小从0(无缓冲),1(行缓冲)到缓冲区大小的正整数(以字节为单位)
您在encoding
中使用open()
与错误消息所说的完全相同 - an invalid keyword argument for this function