首先需要使用之前抓取的数据
首先读取文件,由于只需要标题,弹幕数,播放量和评论数,而且要找到标题和弹幕数、播放量、评论数排名的对应关系,所以新建字典,以标题作为key,播放量为value(以播放量为例,其他两个也是以标题作为key值)。
import csv
play_dict = {}
comment_dict = {}
rcount_dict = {}
new_play_top_list = []
new_comment_top_list = []
new_rcount_top_list = []
#读取文件
with open('video.csv','r',encoding="utf-8") as file:
reader = csv.reader(file)
#只需要标题,弹幕数,播放量,评论数
for row in reader:
#print("title:",row[1])
#赋值,play_dict,comment_dict,rcount_dict都是字典
play_dict[row[1]] = int(row[3])
comment_dict[row[1]] = int(row[4])
rcount_dict[row[1]] = int(row[5])
此时获取了key和value的对应,所以需要根据value值对应排序
new_play_top_list = sorted(play_dict.items(),key = lambda kv:(kv[1], kv[0]),reverse = True)
new_comment_top_list = sorted(comment_dict.items(),key = lambda kv:(kv[1], kv[0]),reverse = True)
new_rcount_top_list = sorted(rcount_dict.items(),key = lambda kv:(kv[1], kv[0]),reverse = True)
print("播放量Top10")
print(new_play_top_list[0:10])
print("评论数Top10")
print(new_comment_top_list[0:10])
print("弹幕数Top10")
print(new_rcount_top_list[0:10])
输出为
全部代码
import csv
play_dict = {}
comment_dict = {}
rcount_dict = {}
new_play_top_list = []
new_comment_top_list = []
new_rcount_top_list = []
with open('video.csv','r',encoding="utf-8") as file:
reader = csv.reader(file)
for row in reader:
#print("title:",row[1])
play_dict[row[1]] = int(row[3])
comment_dict[row[1]] = int(row[4])
rcount_dict[row[1]] = int(row[5])
new_play_top_list = sorted(play_dict.items(),key = lambda kv:(kv[1], kv[0]),reverse = True)
new_comment_top_list = sorted(comment_dict.items(),key = lambda kv:(kv[1], kv[0]),reverse = True)
new_rcount_top_list = sorted(rcount_dict.items(),key = lambda kv:(kv[1], kv[0]),reverse = True)
print("播放量Top10")
print(new_play_top_list[0:10])
print("评论数Top10")
print(new_comment_top_list[0:10])
print("弹幕数Top10")
print(new_rcount_top_list[0:10])