0

I am trying to teach myself Python. This might be a newbie question. This heatmap is one of the projects I've found and am trying to fix to try and learn.

This is the code. It's one of three scripts involved with the project. I will post the other two if needed. I keep getting TypeError: Can't convert bytes object to string implicitly from line 156 (fourth line from the bottom). I get no other errors.

I'm running it in python 3. I've already made a few corrections to update the code...especially to the print commands. (I added the parentheses)

Any help with the error I'm getting would be immensely appreciated.

import tweepy
from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener

import geopy
from geopy.geocoders import Nominatim
geolocator = Nominatim()

from textblob import TextBlob

#declare variables
ckey = 'x'
csecret = 'x'
atoken = 'x'
asecret = 'x'

OAUTH_KEYS = {'consumer_key':ckey, 'consumer_secret':csecret, 'access_token_key':atoken, 'access_token_secret':asecret}
auth = tweepy.OAuthHandler(OAUTH_KEYS['consumer_key'], OAUTH_KEYS['consumer_secret'])
api = tweepy.API(auth)

#open file
saveFile = open('file.csv', 'a')
headers = 'Time; User; Text; Sentiment; Question; Place; Latitude; Longitude;'
saveFile.write(headers + '\n')

#print tweets
for tweet in tweepy.Cursor(api.search, q ='#pittsburgh').items():
    '''
    Parameters (*required):
    q*: word, hashtag of interest [#hashtag]
    geocode: returns tweets by users located within a given radius of the given latitude/longitude [37.781157,-122.398720,1km]
    lang: language [es]
    result_type: [mixed, popular or recent]
    count: [15-100]
    until: YYYY-MM-DD, 7-day limit [2015-12-5]
    since_id: returns results with an ID greater than (that is, more recent than) the specified ID [12345]
    '''

    if tweet.coordinates:
        print("===========")
        #date & time
        moment = tweet.created_at
        print('Date & Time: ', moment)
        #text
        string = tweet.text.replace('|', ' ')
        string = tweet.text.replace('\n', ' ')
        print('Text: ', string.encode('utf8'))
        #user
        user = tweet.author.name.encode('utf8')
        print('User:', user)
        #sentiment
        text = TextBlob(string)
        if text.sentiment.polarity < 0:
            sentiment = "negative"
        elif text.sentiment.polarity == 0:
            sentiment = "neutral"
        else:
            sentiment = "positive"
        print('Sentiment: ', sentiment)
        #question
        if '?' in string:
            question = 'Yes'
        else:
            question = 'No'
        print('Question: ', question)
        #location    
        place = geolocator.reverse(str(tweet.coordinates))
        print('Place: ', place)
        latitude = tweet.coordinates[0]
        longitude = tweet.coordinates[1]
        print('Coords:', tweet.coordinates)
        print("===========")

        #save tweets
        saveThis = str(moment) + '; ' + str(string.encode('ascii', 'ignore')) + '; ' + user + '; ' + sentiment + '; ' + question + '; ' + place + '; ' + str(latitude) + '; ' + str(longitude) + '; '
        saveFile.write(saveThis + '\n')

    if tweet.place:
        print("===========")
        #date & time
        moment = tweet.created_at
        print('Date & Time: ', moment)
        #text
        string = tweet.text.replace('|', ' ')
        string = tweet.text.replace('\n', ' ')
        print('Text: ', string.encode('utf8'))
        #user
        user = tweet.author.name.encode('utf8')
        print('User: ', user)
        #sentiment
        text = TextBlob(string)
        if text.sentiment.polarity < 0:
            sentiment = "negative"
        elif text.sentiment.polarity == 0:
            sentiment = "neutral"
        else:
            sentiment = "positive"
        print('Sentiment: ', sentiment)
        #question
        if '?' in string:
            question = 'Yes'
        else:
            question = 'No'
        print('Question: ', question)
        #location
        place = tweet.place.full_name
        print('Place:', place)
        location = geolocator.geocode(place)
        latitude = location.latitude
        longitude = location.longitude
        print('Coords: ', location.latitude, location.longitude)
        print("===========")

        #save tweets
        saveThis = str(moment) + '; ' + str(string.encode('ascii', 'ignore')) + '; ' + user + '; ' + sentiment + '; ' + question + '; ' + place + '; ' + str(latitude) + '; ' + str(longitude) + '; '
        saveFile.write(saveThis + '\n')

    else:
        print("===========")
        #date & time
        moment = tweet.created_at
        print('Date & Time', moment)
        #text
        string = tweet.text.replace('|', ' ')
        string = tweet.text.replace('\n', ' ')
        print('Text:', string.encode('utf8'))
        #user
        user = tweet.author.name.encode('utf8')
        print('User:', user)
        #sentiment
        text = TextBlob(string)
        if text.sentiment.polarity < 0:
            sentiment = "negative"
        elif text.sentiment.polarity == 0:
            sentiment = "neutral"
        else:
            sentiment = "positive"
        print('Sentiment:', sentiment)
        #question
        if '?' in string:
            question = 'Yes'
        else:
            question = 'No'
        print('Question:', question)
        #location
        place = ''
        latitude = ''
        longitude = ''
        print('Place:', place)
        print('Coords:', latitude, longitude)
        print("===========")

        #save tweets
        saveThis = str(moment) + '; ' + str(string.encode('ascii', 'ignore')) + '; ' + user + '; ' + sentiment + '; ' + question + '; ' + place + '; ' + str(latitude) + '; ' + str(longitude) + '; '
        saveFile.write(saveThis + '\n')

#close file
saveFile.close()
mpellas
  • 11
  • 1
  • Could it be that the original project was in Python 2.7 (and you changed the print statements)? – Jacques de Hooge Aug 05 '16 at 19:38
  • I suspect that it is because, e.g., `user = tweet.author.name.encode('utf8')` returns type `` the concatenation of which with the other string parts in the mentioned line fails... – ewcz Aug 05 '16 at 19:52
  • @JacquesdeHooge Thank you. I learned something new already. I eliminated the parentheses in the print statements, updated the needed libraries in Python 2.7 and it's worked better than it has so far. I am now getting an error connected to this line: place = geolocator.reverse(str(tweet.coordinates)). The error being ValueError: must be a coordinate pair or point. – mpellas Aug 05 '16 at 20:00
  • @ewcz Thanks! I was looking at that line last night as the line returning the bytes and causes the error. (yay! I was on the right track!) lol – mpellas Aug 05 '16 at 20:04

0 Answers0