output file of the code. 我在输出文本文件中将time_zone和UTC_offset的值设置为null。对于每个tweet,我都需要一个非null值和不同的值,如果某条tweet是印度某人发布的,则UTC_offset和该tweet的time_zone与在美国发布的tweet有所不同,我需要这些非null值。如果我使用dataframe.to_json转换,我会得到tweet.created_at格式错误,但如果我使用dataframe则不是这种情况.to_csv转换。有人可以解释一下吗?
python和tweepy的P.S-Beginner

import pandas as pd
from datetime import datetime, date, time, timedelta
import json
from dateutil.tz import tzoffset

# Variables that contains the user credentials to access Twitter API
consumer_key = 'mine'
consumer_secret = 'mine'
access_token = 'mine'
access_token_secret = 'mine`enter code here`'

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)


api = tweepy.API(auth)
# Open/Create a file to append data
#csvFile = open('tweet.txt', 'a')
  #Use csv Writer
  #csvWriter = csv.writer(csvFile,delimiter=',')
  results=[]

for tweet in tweepy.Cursor(api.search, q="Depression" or "Anxiety",lang="en").items(30):
 if (not tweet.retweeted) and ('RT @' not in tweet.text):
 results.append(tweet)


 def tweets_df(results):
     id_list = [tweet.id for tweet in results]
     data_set = pd.DataFrame(id_list, columns=["id"])
     data_set["text"] = [tweet.text for tweet in results]
     data_set["source"] = [tweet.source for tweet in results]
     data_set["screen_name"] = [tweet.user.screen_name for tweet in results]

     #localtime_tz = tzoffset(user.time_zone, user.utc_offset)
     data_set["created_at"] = [tweet.user.created_at for tweet in results]
    # data_set["place"]=[tweet.place for tweet in results]
     data_set["location"] = [tweet.user.location for tweet in results]
     data_set["UTC_Offset"] = [tweet.user.utc_offset for tweet in results]
     data_set["timezone"] = [tweet.user.time_zone for tweet in results]
    # data_set["year"] = [tweet.created_at.year for tweet in results]
    # data_set["month"] = [tweet.created_at.month for tweet in results]
     #data_set["day"] = [tweet.created_at.day for tweet in results]
     #data_set["hour"] = [tweet.created_at.hour for tweet in results]
     return data_set


 data_set = tweets_df(results)
 #data_set.to_csv("/home/rajneeshkaushal/Documents/Pycharm/hived/tweet_data.txt",header=None)
 out = data_set.to_json(orient='records')[1:-1].replace('},{', '} {')
 with open('test.txt', 'w') as f:
     f.write(out)

最佳答案

我想此链接部分回答了您的问题:https://twittercommunity.com/t/upcoming-changes-to-the-developer-platform/104603

根据新的欧盟隐私权新法律,5月23日之后,Twitter用户对象中的时区值将变为私有字段。

关于python - 获取time_zone和utc_offset的空值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50830126/

10-12 21:17