从redis中读取数据统计并将其保存在excel中

2018-02-09  本文已影响0人  tkpy
# -*- coding: utf-8 -*-
import sys
reload(sys)
from collections import Counter
import redis,json
import xlwt

# i = 1
while True:
# while i<3:
#     i+=1
    # pool = redis.ConnectionPool(host='127.0.0.1', port=6379, db=0)
    pool = redis.ConnectionPool(host='r-2zefc71473d249c4.redis.rds.aliyuncs.com', port=6379,password='zhugeZHAOFANG1116', db=0)
    r = redis.Redis(connection_pool=pool)
    m = raw_input("请输入城市(拼音):")
    print('写入中>>>>>>>>>>>>>>>')
    # key = 'changsha-fangcompany-details'
    key = str(m)+'-anjukecompany-details'

    count  = r.llen(key)
    # print count
    result = r.lrange(key, 0, -1)
    companyList = []
    for i in result:
        i = json.loads(i)
        if i.get("data").get("company_name") != '':
            companyList.append(i.get("data").get("company_name"))
            companyList.append(i.get("data").get("company_url"))

    topTen = Counter(companyList)

    topTen = sorted(topTen.items(), key=lambda items:items[1], reverse=True)
    # print(topTen)
    # print json.dumps(topTen,ensure_ascii=False)
    style = xlwt.XFStyle()
    font = xlwt.Font()
    font.name = 'SimSun'    # 指定“宋体”
    style.font = font
    # 创建workbook和sheet对象
    workbook = xlwt.Workbook(encoding='utf-8') #注意Workbook的开头W要大写
    sheet1 = workbook.add_sheet('sheet1',cell_overwrite_ok=True)
    #向sheet页中写入数据
    m1 = raw_input("请输入城市(汉字):")

    for i in range(len(topTen)):
        item = topTen[i]
        if i < 41:
            # sheet1.write(1,0,"长沙")
            sheet1.write(1,0,str(m1))

            sheet1.write(i+1,0,json.dumps(item,ensure_ascii=False))
    """
    #-----------使用样式-----------------------------------
    #初始化样式
    style = xlwt.XFStyle() 
    #为样式创建字体
    font = xlwt.Font()
    font.name = 'Times New Roman'
    font.bold = True
    #设置样式的字体
    style.font = font
    #使用样式
    sheet.write(0,1,'some bold Times text',style)
    """
    #保存该excel文件,有同名文件时直接覆盖
    # workbook.save('C:\\Users\\Administrator\\Desktop\\ww\\changsha.xls')
    workbook.save('C:\\Users\\Administrator\\Desktop\\ww\\%s.xls'%m)

    print '创建excel文件完成OK!!!!'
上一篇下一篇

猜你喜欢

热点阅读