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()
Related
Decided to make a simple mp3 player for terminal. But while I was doing animation I had a problem - it blinks when the frame changes. Heres a video of it: https://youtu.be/in4VLPOfzHw. And the code:
import time, os, glob, eyed3, math, sys
from colorama import init
from mutagen.mp3 import MP3
mpts = glob.glob('*.mp3')
dark_grey = '\033[1;30;40m'
light_grey = '\033[0;37;40m'
white = '\033[1;37;40m'
lime = '\033[1;32;40m'
red = '\033[0;31;40m'
i = 0
song_list = []
for mpt in mpts:
song = MP3(mpt)
duration = math.ceil(song.info.length)
m_duration = duration // 60
s_duration = duration % 60
song = eyed3.load(mpt)
name = song.tag.title
song_list.append([name, [m_duration, s_duration]])
init()
# draw
while True:
# cassette
res = ''
i += 1
res += light_grey + ' ■̅̅̅̅̅̅̅̅̅̅̅̅■ \n'
res += dark_grey + ' |'
res += light_grey + '|############|'
res += dark_grey + '| \n'
res += dark_grey + ' |'
res += light_grey + '|'
if i % 4 == 0:
res += white + ' (/)====(/) '
elif i % 4 == 1:
res += white + ' (-)====(-) '
elif i % 4 == 2:
res += white + ' (\\)====(\\) '
elif i % 4 == 3:
res += white + ' (|)====(|) '
res += light_grey + '|'
res += dark_grey + '| \n'
res += dark_grey + ' |'
res += light_grey + '|############|'
res += dark_grey + '|\n'
res += light_grey + ' ■____________■ \n'
# green line
res += lime + ' ___________________________________\n\n'
# song list
res += red + ' # NAME TIME\n'
for i1 in range(len(song_list)):
res += dark_grey + ' ' + str(i1+1) + '.'
res += white + ' ' + song_list[i1][0] + ' '*(28 - len(song_list[i1][0])) + f'{song_list[i1][1][0]}:{song_list[i1][1][1]}\n'
os.system('cls')
sys.stdout.write(res)
sys.stdout.flush()
time.sleep(0.4)
Can it be fixed or sould I try to make in some other language instead of python?
It's the shelling out to cls that's doing it. Since you're already using ANSI codes for other stuff, try something like:
clear = '\033c'
...
while True:
...
print(clear)
Note that you'll never be able to completely get rid of the screen flicker using the "clear the screen then redraw it" technique, but this will shave several milliseconds from every loop and should decrease the flickering.
The idea is to avoid cleaning the whole screen (os.system('cls')). We could simply move the cursor to top and reprint everything. However moving cursor to top is almost impossible. One workaround I found is to print a lot of special characters that move cursor up one line until all the way to the top.
Reference:
cmd console game; reduction of blinking
The first solution of using \b does not work for me on a windows machine. So I go for the ender_scythe's solution. You have to print an empty line on first run to avoid the issue he/she mentioned. Here is the sample code that does not blink at all:
import time
import os
i = 0
dark_grey = '\033[1;30;40m'
light_grey = '\033[0;37;40m'
white = '\033[1;37;40m'
lime = '\033[1;32;40m'
red = '\033[0;31;40m'
def my_cls(nrow = 0):
if nrow == 0:
os.system('cls')
else:
print('\033[F'*nrow)
def my_display(chars):
print(''.join(chars))
return len(chars)
nrow = 0
while True:
my_cls(nrow)
# cassette
res = []
i+=1
if i == 1:
res.append('\n')
res.append(light_grey + ' ■̅̅̅̅̅̅̅̅̅̅̅̅■ \n')
res.append(dark_grey + ' |')
res.append(light_grey + '|###########|')
res.append(dark_grey + '| \n')
res.append(dark_grey + ' |')
res.append(light_grey + '|')
if i % 4 == 0:
res.append(white + ' (/)====(/) ')
elif i % 4 == 1:
res.append(white + ' (-)====(-) ')
elif i % 4 == 2:
res.append(white + ' (\\)====(\\) ')
elif i % 4 == 3:
res.append(white + ' (|)====(|) ')
res.append(light_grey + '|')
res.append(dark_grey + '| \n')
res.append(dark_grey + ' |')
res.append(light_grey + '|############|')
res.append(dark_grey + '|\n')
res.append(light_grey + ' ■____________■ \n')
# green line
res.append(lime + ' ___________________________________\n\n')
# song list
res.append(red + ' # NAME TIME\n')
nrow = my_display(res)
time.sleep(0.4)
I'm trying to send an email to someone with information I've scraped from the web but I can't get the contents to send. I keep receiving empty emails. Any help would be great. I've tried all sorts of different numbers of ' and +s and i can't figure it out.
def singaporeweather():
singaporehigh=singapore_soup.find(class_='tab-temp-high').text
singaporelow=singapore_soup.find(class_='tab-temp-low').text
print('There will be highs of ' + singaporehigh + ' and lows of ' +
singaporelow + '.')
def singaporesuns():
singaporesunsets=singapore_soup.find(class_='row col-sm-5')
suns_singapore=singaporesunsets.find_all('time')
sunset_singapore=suns_singapore[1].text
sunrise_singapore=suns_singapore[0].text
print('Sunrise: ' + sunrise_singapore)
print('Sunset: ' + sunset_singapore)
def ukweather():
ukhigh= uk_soup.find('span', class_='tab-temp-high').text
uklow= uk_soup.find(class_='tab-temp-low').text
print('There will be highs of ' + ukhigh + ' and lows of ' + uklow +
'.')
def uksuns():
uk_humid = uk_soup.find('div', class_='row col-sm-5')
humidity=uk_humid.find_all('time')
sunrise_uk=humidity[0].text
sunset_uk= humidity[1].text
print('Sunrise: '+str(sunrise_uk))
print('Sunset: '+str(sunset_uk))
def ukdesc():
uk_desc=uk_soup.find('div',class_='summary-text hide-xs-only')
uk_desc_2=uk_desc.find('span')
print(uk_desc_2.text)`enter code here`
def quotes():
quote_text=quote_soup.find(class_='b-qt qt_914910 oncl_q').text
author=quote_soup.find(class_='bq-aut qa_914910 oncl_a').text
print('Daily quote:\n' + '\"'+quote_text +'\"'+ ' - ' + author +'\n')
def message():
print('Subject:Testing\n\n')
print(('Morning ' +
nameslist[random.randint(1(len(nameslist)-1))]).center(30,'*'),
end='\n'*2)
quotes()
print('UK'.center(30,'_') + '\n')
ukweather()
ukdesc()
uksuns()
print('\n' + 'Singapore'.center(30,'_') + '\n')
singaporeweather()
singaporedesc()
singaporesuns()
smtpthing.sendmail('XXX#outlook.com', 'XXX#bath.ac.uk', str(message()))
In your functions, instead of printing the results to the console, you should use return statements so that you can use the function's result in your main program. Otherwise, message() is returning null, which is why your email is empty (the main program cannot see message()'s result unless it is returned).
Try something like:
def singaporeweather():
singaporehigh=singapore_soup.find(class_='tab-temp-high').text
singaporelow=singapore_soup.find(class_='tab-temp-low').text
return 'There will be highs of ' + singaporehigh + ' and lows of ' +
singaporelow + '.'
By using a return statement like this one, you will be able to use singaporeweather()'s result in your main program, e.g.:
var result = singaporeweather()
Using returns in the rest of your methods as well, you will be able to do the following in your function message():
def message():
body = "" #your message
body += 'Subject:Testing\n\n'
body += ('Morning ' + nameslist[random.randint(1(len(nameslist)-1))]).center(30,'*')
body += quotes()
body += 'UK'.center(30,'_') + '\n'
+ ukweather()
+ ukdesc()
+ uksuns()
+ '\n' + 'Singapore'.center(30,'_') + '\n'
+ singaporeweather()
+ singaporedesc()
+ singaporesuns()
#finally, don't forget to return!
return body
Now you are returning body, now you can use message()'s result in your main program to send your email correctly:
smtpthing.sendmail('XXX#outlook.com', 'XXX#bath.ac.uk', str(message()))
I tried running this code and it gave me this error, "TypeError: can only concatenate str (not "NoneType") to str". I need to know what to do.
I tried using '' or "True" instead of "None"
users = {'zacc_eli':{'first_name': 'Zaccheus',
'middle_name': None,
'last_name': 'Elisha',
'age': 19},
'_Djvy_': {'first_name': 'daniel',
'middle_name': 'joshua',
'last_name': 'adebayo',
'age': None}}
for username, details in users.items():
print(username + ':')
full_name = details['first_name'] + ' ' + details['middle_name'] + ' ' + details['last_name']
full_name2 = details['first_name'] + ' ' + details['last_name']
age = details['age']
if details['middle_name'] == None :
print('\tFull Name: ' + full_name2.title())
else:
print('\tFull Name: ' + full_name.title())
if details['age'] != None:
print('\tAge: ' + str(age))
I expect no double space when there is no middle name and no age when there is no age.
This line
full_name = details['first_name'] + ' ' + details['middle_name'] + ' ' + details['last_name']
fails if details['middle_name'] is None rather than a str value. At this point, though, you haven't checked if that is the case. You don't do that until 4 lines later.
Python is not lazy (like Haskell); it doesn't wait until you actually use the value of full_name to evaluate the expression assigned to it.
Instead, check the value before you do anything that requires a str:
for username, details in users.items():
print(username + ':')
first = details['first_name']
middle = details['middle_name']
last = details['last_name']
if middle is None:
full_name = first + ' ' + last
else:
full_name = first + ' ' + middle + ' ' + last
# Or a one-liner
# full_name = ' '.join([for x in [first, middle, last] if x is not None])
age = details['age']
print('\tFull Name: ' + full_name.title())
if age is not None:
print('\tAge: ' + str(age))
I need to convert any sentence to question using spacy in python.
My Code below goes so long and i need even more to be done to complete any sentence into question format. Now in this code I make condition based on be form, need form, have form, do form by checking past tense and present tense.
INPUT: Nina plays the violin.
OUTPUT: Does Nina play the violin?
INPUT: Barbara gave me the chocolates.
OUTPUT: Who gave you the chocolates?
INPUT: He is seeing Joe tomorrow.
OUTPUT: Who is he seeing tomorrow?
INPUT: She comes from Madrid.
OUTPUT: Where does she come from?
Anybody can help me in this! Want to form question for all type of sentence?
from textacy.spacier import utils
import spacy
nlp = spacy.load("en_core_web_sm")
inp = input()
doc = nlp(inp)
string,label = [],""
for sentence in doc.sents:
root = sentence.root
for i in sentence.ents:
if len(utils.get_subjects_of_verb(root)) or len(utils.get_objects_of_verb(root)) > 0:
label = i.label_
print(root.tag_)
print(root.lemma_)
print(label)
if len(utils.get_subjects_of_verb(root)) > 0:
if root.lemma_ == 'be':
if label == "PERSON" :
ques = 'Who ' + str(root)+" "+ str(utils.get_subjects_of_verb(root)[0]) +' ?'
elif label == "QUANTITY":
ques = 'How ' + str(root)+" "+ str(utils.get_subjects_of_verb(root)[0]) +' ?'
elif label == "MONEY":
ques = 'How much ' + str(root) + " " + str(utils.get_subjects_of_verb(root)[0]) + ' ?'
elif label == "TIME":
ques = 'When ' + str(root)+" "+ str(utils.get_subjects_of_verb(root)[0]) +' ?'
elif label == "GPE":
ques = 'Where ' + str(root)+" "+ str(utils.get_subjects_of_verb(root)[0]) +' ?'
elif label == 'PRODUCT':
ques = 'What ' + str(root)+" "+ str(utils.get_subjects_of_verb(root)[0]) +' ?'
string.append(ques)
print(string)
OR IN ANOTHER WAY
Works for these type of formats:
This is for John - Who is this for?
He was watching a film - What was he watching?
Sam will be back on Friday - When will Sam be back?
They were walking fast - How were they walking?
She left because it was late - Why did she leave?
from textacy.spacier import utils
from textacy.spacier import utils as spacy_utils
import spacy
import re
nlp = spacy.load("en_core_web_sm")
-inp = input()
-doc = nlp(inp)
-stop,modal_root,form_root,root = "","","",""
-
-
-for sentence in doc:
- if sentence.dep_ in ['aux','ROOT']:
- if sentence.lemma_ in ['must', 'shall', 'will', 'should', 'would', 'can', 'could', 'may','might']:
- modal_root = sentence
- elif sentence.lemma_ in ['be','have']:
- form_root = sentence
- else:
- root = sentence
- for children in sentence.subtree:
- if children.text in ['because', 'so because']:
- check = children
- if children.dep_ in ['dobj','pobj','advmod','acomp']:
- child = children
- for prep in child.subtree:
- if prep.is_stop:
- stop = prep
- if child.ent_type_:
- label = child.ent_type_
- elif child.pos_ == "ADV":
- label = "QUANTITY"
- else:
- label = ""
-
-if modal_root and form_root:
- root = modal_root
-elif modal_root:
- root = modal_root
-elif form_root:
- root = form_root
-
-
-for lemma in doc:
- if lemma.text in ['we','I']:
- lem = lemma.text
- else:
- lem = ""
-
-if stop:
- sent = doc.text.replace(str(child),"")
- sent = sent.replace(" "+str(stop)+" ", "")
-else:
- sent = doc.text.replace(str(child), "")
-
-if lem: sent = sent.replace(lem, "you")
-
-if root.lemma_ in ['be','have','must', 'shall', 'will', 'should', 'would', 'can', 'could', 'may', 'might']:
- if label == 'PERSON':
- ques = 'who '+str(root) + " " + re.sub('{}'.format(" "+str(root)+" ").lower(), ' ', sent)+'?'
- elif label in ['GPE','LOC']:
- ques = 'where '+str(root) + " " + re.sub('{}'.format(" "+str(root)+" ").lower(), ' ', sent)+'?'
- elif label in ['TIME','DATE']:
- ques = 'when ' + str(root) + " " + re.sub('{}'.format(" "+str(root)+" ").lower(), ' ', sent)+'?'
- elif label in ['QUANTITY']:
- ques = 'How ' + str(root) + " " + re.sub('{}'.format(" "+str(root)+" ").lower(), ' ', sent)+'?'
- else:
- ques = 'what ' + str(root) + " " + re.sub('{}'.format(" " + str(root) + " ").lower(), ' ', sent) + '?'
-else:
- if root.tag_ == 'VBD' or str(utils.get_subjects_of_verb(root)[0]).upper() in ['I', 'You', 'We', 'They', 'He', 'She','It']:
- if check.text in ['because','so because']:
- ques = 'Why did ' + str(utils.get_subjects_of_verb(root)[0]) + " " + root.lemma_ + '?'
- else:
- ques = 'Did ' + str(utils.get_subjects_of_verb(root)[0]) + " " + root.lemma_ + '?'
- elif str(utils.get_subjects_of_verb(root)[0]).upper() in ['I', 'You', 'We', 'They']:
- ques = 'Do ' + str(utils.get_subjects_of_verb(root)[0]) + " " + root.lemma_ + '?'
- elif str(utils.get_subjects_of_verb(root)[0]).upper() in ['He', 'She', 'It'] or label == "PERSON":
- ques = 'Does ' + str(utils.get_subjects_of_verb(root)[0]) + " " + root.lemma_ + '?'
-
-print(ques)
-
-
HOW TO ACHIEVE CONVERTING SENTENCE INTO QUESTION USING SPACY LIBRARY IN PYTHON?
Trying to get a self updating speedometer and clock working for my truck using gps. So far I have been able to get the read out that I want using easygui and msgbox but it is not self updating which will not help much on either application. Below is the code. Any help would be much appreciated, I know this is pretty ugly and probably not correct but I am new to python.
import gps
from easygui import *
import sys
# Listen on port 2947 (gpsd) of localhost
session = gps.gps("localhost", "2947")
session.stream(gps.WATCH_ENABLE | gps.WATCH_NEWSTYLE)
while True:
try:
report = session.next()
if report['class'] == 'TPV':
if hasattr(report, 'time'):
hour = int(report.time[11:13])
hourfix = hour - 7
if hourfix < 12:
time = 'Current Time Is: ' + report.time[5:7] + '/' + report.time[8:10] + '/' + report.time[0:4] + ' ' + str(hourfix) + report.time[13:19] + ' am'
else:
hourfix = hourfix - 12
time = 'Current Time Is: ' + report.time[5:7] + '/' + report.time[8:10] + '/' + report.time[0:4] + ' ' + str(hourfix) + report.time[13:19] + ' pm'
if report['class'] == 'TPV':
if hasattr(report, 'speed'):
speed = int(report.speed * gps.MPS_TO_MPH)
strspeed = str(speed)
currentspeed = 'Current Speed Is: ' + strspeed + ' MPH'
msgbox(time + "\n" + currentspeed, "SPEEDO by Jono")
except KeyError:
pass
except KeyboardInterrupt:
quit()
except StopIteration:
session = None
print "GPSD has terminated"