# -*- coding: utf-8 -*-
import cx_Oracle
import pandas as pd
connection = cx_Oracle.connect("***",
"***",
"14.29.*.*:11526/orcl",
encoding = "GBK",
nencoding = "GBK")
sql = '''
SELECT DISTINCT t1.vodid,
t1.serviceid,
t1.add_pack_plan_id,
t1.add_pack_plan_name
FROM hsbi.sp_mk_product_content t1
LEFT JOIN iptv_app.test_add_pack_plan t2 ON t1.add_pack_plan_id=t2.add_pack_plan_id
WHERE t2.class_name1='影视'
AND sp_name='南传'
'''
film_df = pd.read_sql(sql, connection)
# 类型转换
film_df.vodid = film_df.vodid.astype(object)
# 将数据存为 pickle
pickle_path = u'/datas/film.pkl'
data.to_pickle(pickle_path)
# 读取 pickle
film_df = pd.read_pickle(pickle_path)
print(film_df.dtypes)
filmdata = dict()
for _, row in film_df.iterrows():
contend_id = str(row['vodid'])
plan_name = str(row['add_pack_plan_name'])
if filmdata.get(contend_id, -1) == -1:
filmdata[contend_id]=set()
filmdata[contend_id].add(plan_name)
# 按照字典的 value 的长度由大到小排列
sorted_filmdata = sorted(filmdata.items(), key=lambda film : len(film[1]), reverse=True)
outpath = u'/datas/film_result.data'
# 将结果写入文件
with open(outpath, 'a+', encoding='utf-8') as fout:
for k, items in sorted_filmdata:
if len(items) >= 2:
print(k + ',' + ','.join(items), file=fout)
print("----------------process ends-------------------")
注意写入文件的代码
with open(outpath, 'a+', encoding='utf-8') as fout:
for k, items in sorted_filmdata:
if len(items) >= 2:
print(k + ',' + ','.join(items), file=fout)
注意sorted 的用法
sorted_filmdata = sorted(filmdata.items(), key=lambda film : len(film[1]), reverse=True)
网友评论