Using the Twitter API

Preparing Results for Analysis

Great! Now that we have the text we need, it's time to prepare it so that we can send it to the Personality Insights (PI) API for analysis.

First, we'll concatenate the text into one long string and then send it off to PI be analyzed. We'll save the long string into a variable called text.

We also only want the tweets that are in English, so we'll need to filter by language.

Note: The text retrieved from Twitter is in Unicode format, but we need UTF-8 format, so we'll need to encode it. Thankfully, the encode() method in Python solves that problem, so we'll use that.

Here's how the modified code looks:

statuses = twitter_api.GetUserTimeline(screen_name=handle, count=200, include_rts=False) text = "" for status in statuses: if (status.lang =='en'): #English tweets text += status.text.encode('utf-8')

The code above does the following:

  1. Creates an empty text variable we'll save tweets into
  2. Filters out English tweets
  3. Appends UTF-8 encoded tweets to the text variable using the += operator
Community Forums
Get help and ask questions in the Codecademy Forums
Report a Bug
If you see a bug or any other issue with this page, please report it here.