进度条
import sys, time class ShowProcess(object): """ 显示处理进度的类 调用该类相关函数即可实现处理进度的显示 """ # i = 0 # 当前的处理进度 # max_steps = 0 # 总共需要处理的次数 # max_arrow = 50 #进度条的长度 # 初始化函数,需要知道总共的处理次数 def __init__(self, max_steps): self.max_steps = max_steps # 总共需要处理的次数 self.max_arrow = 50 # 进度条的长度 self.i = 0 # 当前的处理进度 # 显示函数,根据当前的处理进度i显示进度 # 效果为[>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>]100.00% def show_process(self, i=None): if i is not None: self.i = i num_arrow = int(self.i * self.max_arrow / self.max_steps) # 计算显示多少个'>' num_line = self.max_arrow - num_arrow # 计算显示多少个'-' print(num_arrow,">") print(num_line,"-") percent = self.i * 100.0 / self.max_steps # 计算完成进度,格式为xx.xx% print(percent,'百分比') process_bar = ' ' + '[' + '>' * num_arrow + '-' * num_line +']' + '%.2f' % percent + '%' #带输出的字符串,' '表示不换行回到最左边 sys.stdout.write(process_bar) # 这两句打印字符到终端 sys.stdout.flush() self.i += 1 def close(self, words='done'): print('') print(words) self.i = 1 if __name__ == '__main__': max_steps = 1000 process_bar = ShowProcess(max_steps) for i in range(max_steps + 1): process_bar.show_process() time.sleep(0.05) process_bar.close()
2.插入对应省份与对应城市的数据
import pymysql import json import datetime conn = pymysql.connect( host='localhost', port=3306, user='root', password='root', database='ssm', charset='utf8' ) cursor = conn.cursor() fp = open('pc-code.json',encoding='utf-8') city_dict = json.load(fp) province_sql = """insert into bd_dic_local_province (name,type,update_date,create_date,delete_flag) values('%s',2,'%s','%s',0)""" city_sql = "insert into bd_dic_local_city(name,province_id,update_date,create_date,delete_flag) values('%s', %s, '%s', '%s',0)" for city in city_dict: try: province_name = city.get('name') cursor.execute(province_sql%(province_name,datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))) province_id = cursor.lastrowid child = city.get('childs') if len(child) > 0: for item in child: city_data = (item.get('name'), province_id,datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")) cursor.execute(city_sql%city_data) conn.commit() except Exception: conn.rollback() fp.close() conn.close()