我在使用以下代码时,出现了TypeError: Object of type 'generator' is not JSON serializable的错误
# -*- coding:utf-8 -*-
#!/usr/bin/python3
import os
import sys
from mrjob.job import MRJob
from mrjob.step import MRStep
import json
import heapq
#统计看电影最多的一个人所看的最新5部电影
class GetVideos(MRJob):
def steps(self):
return[ MRStep(mapper = self.mymapper_1,combiner = self.mycombiner_1,reducer = self.myreducer_1),
MRStep(reducer=self.myreducer_2),MRStep(reducer=self.myreducer_3)]
def mymapper_1(self, _, line):
try:
jsline = json.loads(line)
cuid = jsline["user_id"]
timestmp = jsline["timestamp"]
action = jsline["action"]
video = jsline["data"]
if action == 1:
yield cuid,video
except Exception:
pass
def mycombiner_1(self,key,value):
count=int(len(list(value)))
yield count,(key,value)
def myreducer_1(self,key,value):
yield None,(key,value)
def myreducer_2(self,key,value):
for cnt,key in heapq.nlargest(1,value):
yield key,cnt
def myreducer_3(self,key,value):
vid=key[0]
video=key[1]
video_new=video[-1:-6:-1]
yield vid,video_new
if __name__ == '__main__':
GetVideos.run()
想问一下代码的错误出在哪里,该怎么修改,困扰我一天了呀!
输出一下mymapper_1()函数中 line 的值和类型(type)。
print(type(line),line)
看看是不是字符串类型,内容是不是正确的json格式
您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~
如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~
ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632
非常感谢您使用有问必答服务,为了后续更快速的帮您解决问题,现诚邀您参与有问必答体验反馈。您的建议将会运用到我们的产品优化中,希望能得到您的支持与协助!
速戳参与调研>>>https://t.csdnimg.cn/Kf0y