前言
一直从事php+jquery+layui+Echarts作为数据可视化大屏的前后端开发,包含高德和百度地图API的二次开发。相对来说,php的热度和关注度是低于python的,这样就导致遇见疑难问题的时,只能自行消化(面对CSDN编程)。为了不被淘汰,开始学习python,打算基于python+diango+jquery+layui+Echarts作为新的数据可视化大屏的后端语言开发。
- 作为“拿来主义”的忠实拥趸,python封装函数是基于前期php数据可视化的经验封装进行“迁移”;
- 有点“不求甚解”的味道,只为快速地实现目的;
- 尽可能学好基础,养成优雅代码的好习惯;
- 随时学习随时更新,争取在完成函数封装时对python的基础学习告一段落;
- 随学习进度和知识的积累,随时优化封装代码;
1. 时间戳转时间格式
''' #时间戳转时间格式 #time参数格式,为1489465020 ''' def stampToTime(times): timeArray = time.localtime(times) otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray) return otherStyleTime
2. 将获取的数据转为JSON
''' #将获取的数据转为JSON保存到本地 #content,字典类型 #name,文件名称 #suffix,保存格式后缀名 ''' def makeTxt(content, name="lockdatav", suffix='json'): with open('{}.{}'.format(name, suffix), 'w+') as f: f.write(json.dumps(content, ensure_ascii=False)) makeTxt(data, 'safety', 'json')
3. 读取Excle并转为json
# 导入py库 import os import json import xlrd from datetime import date # Excle时间格式转化为py时间格式 def xlsfpytime(xlsTime): # 防止参数为字符串 if isinstance(xlsTime, str) == False: data_value = xlrd.xldate_as_tuple(xlsTime, 0) tmpTime = date(*data_value[:3]).strftime('%Y-%m-%d') return tmpTime else: pass def xls2json(fileName): # 判断文件是否存在 if os.path.exists(fileName): book = xlrd.open_workbook(fileName) # 根据sheet索引或者名称获取sheet内容 sheet = book.sheet_by_index(0) # 格式化数据,生成Dict tables = [] data = {} for rown in range(sheet.nrows): list = {} list['name'] = sheet.cell_value(rown, 0) list['contacts'] = sheet.cell_value(rown, 1) list['mobile'] = sheet.cell_value(rown, 2) list['address'] = sheet.cell_value(rown, 3) list['category'] = sheet.cell_value(rown, 4) list['scale'] = sheet.cell_value(rown, 5) list['visit'] = xlsfpytime(sheet.cell_value(rown, 6)) # 时间格式的转化 list['capital'] = sheet.cell_value(rown, 7) tables.append(list) # print(tables) # 删除标题头 del tables[0] data['code'] = 0 data['msg'] = 'OK' data['data'] = tables # 转为JSON res = json.dumps(data, ensure_ascii=False) return res else: print('对应的xls表格不存在') # 执行函数调用 fileName = r'C:/Users/Administrator/Desktop/客户推进表.xls' print(xls2json(fileName))
4. python进行md5加密
import time import hashlib def getMd5(str): new_md5 = hashlib.md5() new_md5.update(str.encode(encoding='utf-8')) return new_md5.hexdigest() nowTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) print(getMd5(nowTime))
5. 指定个数的随机字符串(密码)
def getRandPass(nums): str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890" return ''.join(__import__('random').choice(str) for i in range(nums)) print(getRandPass(10))
6. 读取图片的属性信息
import exifread def read(urls): with open(urls, 'rb') as f: contents = exifread.process_file(f) print(contents) urls = "C:/Users/30930/Desktop/IMG_1480.JPG" read(urls)
7. 将数据存储为exlce表格
import os import xlwt """ 生成excle的封装函数 save_path,保存路径 sheetname,工作表名称 column_name_list,列标题,类型:列表 read_list,数据 """ # 保存为Excel文件 def save_excel(save_path, sheetname, column_name_list, read_list): workbook = xlwt.Workbook() sheet1 = workbook.add_sheet(sheetname=sheetname) for i in range(0, len(column_name_list)): sheet1.write(0, i, column_name_list[i]) k = 1 for v in read_list: kval = v.split(',') for j in range(0, len(kval)): sheet1.write(k, j, kval[j]) k = k + 1 workbook.save(save_path) print('信息保存 OK,记录条数共计:' + str(len(read_list))) # 默认保存路径 save_path = "" if save_path == "": save_path = os.getcwd() + "/2.xls" sheetname = "sheet1" column_name_list = ["姓名", "号码"] read_list = ["张三,135", "张三,135"] if save_path == "": save_path = os.getcwd() save_excel(save_path, sheetname, column_name_list, read_list)
8. 获取文件md5值
import hashlib def getMD5(filepath): f = open(filepath, 'rb') md5obj = hashlib.md5() md5obj.update(f.read()) hash = md5obj.hexdigest() f.close() return str(hash).lower() filepath = r"list1.txt" print(getMD5(filepath))
9. 随机字典
import json import random def getRanJson(nums): a = [random.randint(40, 100) for k in range(nums)] print(type(a)) b = json.dumps(a, ensure_ascii=False) print(b) getRanJson(6)
10.随机颜色
import random def random_color(): color_list = ['1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'] color = '' for i in range(6): color += random.choice(color_list) return '#' + color print(random_color())
lockdatav done!