将Excel CSV中的用户ID格式化为指数数字

时间:2017-07-12 15:05:02

标签: python csv data-mining tweepy

我正在使用python tweepy收集twitter数据,这里是代码

class listener (StreamListener):

def on_data(self, raw_data):

    data = json.loads(raw_data)
    print data.keys()
    tweet = data['text'].encode("utf-8")
    tweet_id = data['id']
    time_tweet = data['timestamp_ms']
    date = datetime.datetime.fromtimestamp(int(time_tweet) / 1000)
    new_date = str(date).split(" ") [0]
    print new_date
    user_id = data['user']['id']
    with  open('twitDB.csv','ab') as csvfile:
        myfile = csv.writer(csvfile)
        myfile.writerow([tweet_id,new_date,tweet,user_id])



    return True

def on_error(self, status_code):
    print status_code
auth = OAuthHandler(consumer_key,consumer_secret)
auth.set_access_token(access_token,access_token_secret)
twitterStream = Stream(auth,listener())
twitterStream.filter(track=["car"])

enter image description here

但是在用于tweet_id和user_id的Csv文件中,我是如何解决这个问题的呢?

1 个答案:

答案 0 :(得分:0)

在tweet_id前面放置一个制表符

tweet_id = '\t' + data['id']