Jagadeesh
Jagadeesh

Reputation: 407

UnicodeEncodeError is occurring while extracting Twitter data

I am trying to extract tweets based on Hash-tag. Below is my Python Source Code

import tweepy

consumer_key = "##"
consumer_secret = "##"
access_key = "##"
access_secret = "##"

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)

def main():
    api = tweepy.API(auth)
    search_string = "#cancer"
    search_results = api.search(
        q=search_string, lang='en', count=100, until='2014-12-19')

    for i in search_results:
        print(i)

if __name__ == '__main__':
    main()

This program throws the following UnicodeEncodeError:

Traceback (most recent call last):
  File "C:\Users\Jaggy Paw\Workspace\TwitterDataExtraction\streaming.py", line 26, in <module>
    main()
  File "C:\Users\Jaggy Paw\Workspace\TwitterDataExtraction\streaming.py", line 23, in main
    print(i)
  File "C:\Users\Jaggy Paw\Anaconda3\lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u2708' in position 1229: character maps to <undefined> 

How to resolve this error?

Upvotes: 0

Views: 156

Answers (1)

Jagadeesh
Jagadeesh

Reputation: 407

Yay!!! I solved this issue by myself after working for couple more hours.

As I am running my program on Windows7, there are issues in printing 'utf-8' characters on the console.

So I added the below lines to my code without modifying any part of code( mentioned in the question)

sys.stdout = codecs.getwriter('utf8')(sys.stdout.buffer, 'strict')
sys.stderr = codecs.getwriter('utf8')(sys.stderr.buffer, 'strict')

Upvotes: 1

Related Questions