我想将一个表存储在csv
文件中,其中包含一行和两个变量:
Variable one
应为平均Polarity score
,Variable two
应为平均Subjectivity score
我收到错误:
"UnicodeEncodeError: 'ascii' codec can't encode character u'\u2026' in position 102: ordinal not in range(128)"
tweet.encode("utf-8")
这是print r生成的输出(Bellow,你可以看到我的代码)
Collecting tweets...
Done!
[[u'TILRobot', 'N/A', u'Source: U8\n\n(h/t >> htVs)\n\n#Japan w', 'No', '18:08', '11-09-2017', 103, 0.0, 0.0], [u'Rainmaker1973', u'Italy, North by Northwest', u'RT @Rainmaker1973: Happy 80th #birthday to Bob Crippen, former astronaut & pilot of the first Space Shuttle flight \u2026', 'Yes', '18:07', '11-09-2017', 143, 0.35000000000000003, 0.4444444444444444], [u'_lanzz_', u'London', u"@Wicknes007\nBut yeah, she wasn't a looker, so probably deserves to have a few laughs at her\u2026", 'No', '18:05', '11-09-2017', 140, -0.2, 0.1]]
代码:
import tweepy
from textblob import TextBlob
import csv
def main(query, size):
# Enter your Twitter credentials below
consumer_key = ''
consumer_secret = ''
access_token = ''
access_token_secret = ''
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
# Authenticates with Twitter
api = tweepy.API(auth, wait_on_rate_limit=True)
print("Collecting tweets...")
tweets_array = []
# Iterates through the collected Tweets and extracts the required info before appending them to an array
for tweet in tweepy.Cursor(api.search,
q=query,
result_type="recent",
lang="en").items(size):
userid = api.get_user(tweet.user.id)
username = userid.screen_name
if tweet.user.location:
location = tweet.user.location
else:
location = "N/A"
tweetText = tweet.text
length = len(tweetText)
analysis = TextBlob(tweet.text)
polarity = analysis.sentiment.polarity
subjectivity = analysis.sentiment.subjectivity
datestamp = tweet.created_at
time = datestamp.strftime("%H:%M")
year = datestamp.strftime("%d-%m-%Y")
if ('RT @' in tweet.text):
retweet = "Yes"
else:
retweet = "No"
tweets_array.append([username, location, tweetText, retweet, time, year, length, polarity, subjectivity])
print("Done!")
return tweets_array
def save_to_csv(tweets_list):
print('Saving to CSV')
# Writes out the tweets array to a CSV file
with open("Twitter_Sentiment.csv", "wb") as f:
writer = csv.DictWriter(f, fieldnames=["Username", "Location", "Tweet", "Retweet", "Time", "Year", "Length", "polarity", "subjectivity"], delimiter='|')
writer.writeheader()
writer = csv.writer(f, delimiter='|')
writer.writerows(tweets_list)
if __name__ == '__main__':
try:
tweets_list = main("Wikipedia.org Twitter", 200)
save_to_csv(tweets_list)
except KeyboardInterrupt:
print("Bye!")