How can I modify this code so when senti_avg is not divisible (0/value), reducer() outputs NULL or NONE instead of crashing?
def reducer(self, bs_id, value):
avg_data = list(value)
senti_sum = sum([a[0] for a in avg_data])
word_sum = sum([a[1] for a in avg_data])
senti_avg = senti_sum/float(word_sum)
yield (bs_id, senti_avg)
You use python exception handling :
try:
senti_avg = senti_sum/float(word_sum)
except ZeroDivisionError:
senti_avg = None
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.