码迷,mamicode.com
首页 > 其他好文 > 详细

接手项目代码

时间:2020-11-23 12:38:04      阅读:5      评论:0      收藏:0      [点我收藏+]

标签:from   for   src   line   with   else   str   pen   coding   

原项目代码如下

def data_filter(src=r‘D:\material\data.json‘,des=r‘D:\material\pro_data.json‘,db = r‘D:\material\tnbs.db‘):
    fw = open(des, ‘w‘, encoding=‘utf-8‘)
    with open(src, encoding=‘utf-8‘) as fr:
        lines = fr.readlines()

        legend_num = len(lines)

        line_1 = eval(lines[1].lstrip())
        conn = sqlite3.connect(db)
        cursor = conn.cursor()
        dwg_name = line_1[‘dwg_name‘]

        cursor.execute(‘delete from t_designRecStat‘)
        filtered_num = 0
        for index in range(len(lines)):
            line = eval(lines[index].lstrip())
            pole_info_tag = True
            if ‘wires_info‘ in line[‘pole_info‘].keys():
                for pole_info in line[‘pole_info‘][‘wires_info‘]:
                    if ‘materials_selection‘ not in pole_info.keys():
                        pole_info_tag = False
            else:
                pole_info_tag = False

            if ‘materials_selection‘ in line[‘pole_info‘].keys() and pole_info_tag:
                fw.write(re.sub(‘\‘‘, ‘\"‘, str(line)) + ‘\n‘)
            else:
                filtered_num += 1
        rec_amount = legend_num - filtered_num
        t_designRecStat_sql = ‘insert into t_designRecStat(designName,legendAmount,recAmount,filteredNum) values(‘ + ‘\‘‘ + dwg_name + ‘\‘‘                               ‘,‘ + ‘\‘‘ + str(legend_num) + ‘\‘‘ + ‘,‘ + ‘\‘‘ + str(rec_amount) + ‘\‘‘ + ‘,‘ + ‘\‘‘ + str(filtered_num) + ‘\‘‘ + ‘)‘
        cursor.execute(t_designRecStat_sql)
        conn.commit()
        conn.close()
    fw.close()

修改后

def sqlite_dml(dml_sql, db_name=r‘D:\material\tnbs_2.db‘, seq_of_parameters=‘‘):
    conn = sqlite3.connect(db_name)
    cursor = conn.cursor()
    cursor.executemany(dml_sql, seq_of_parameters)
    conn.commit()
    conn.close()

@timeit
def data_filter(src=r‘D:\material\data.json‘,des=r‘D:\material\pro_data_2.json‘,db = r‘D:\material\tnbs_2.db‘):
    t_designRecStat_sql_prefix = ‘insert into t_designRecStat(designName,legendAmount,recAmount,filteredNum) values‘
    t_designRecStat_sql_params = []
    with open(des, ‘w‘, encoding=‘utf-8‘) as fw:
        with open(src, encoding=‘utf-8‘) as fr:
            lines = fr.readlines()
            legend_num = len(lines)
            line_1 = json.loads(lines[1])
            dwg_name = line_1[‘dwg_name‘]
            filtered_num = 0
            for line in lines:
                line = json.loads(line)
                pole_info_tag = all((‘materials_selection‘ in line[‘pole_info‘].keys(), all([‘materials_selection‘ in pole_info.keys() for pole_info in line[‘pole_info‘][‘wires_info‘]]) if ‘wires_info‘ in line[‘pole_info‘].keys() else False))
                if pole_info_tag:
                    json.dump(line, fw, ensure_ascii=False)
                else:
                    filtered_num += 1
            rec_amount = legend_num - filtered_num
            params_list = [dwg_name, legend_num, rec_amount, filtered_num]
            params = ‘(‘+‘,‘.join(map(lambda s: "‘" + s + "‘" if isinstance(s, str) else str(s), params_list))+‘)‘
            t_designRecStat_sql_params.append(params)
        t_designRecStat_sql = t_designRecStat_sql_prefix + ‘,‘.join(t_designRecStat_sql_params)
        sqlite_dml(‘delete from t_designRecStat‘)
        sqlite_dml(t_designRecStat_sql)

接手项目代码

标签:from   for   src   line   with   else   str   pen   coding   

原文地址:https://www.cnblogs.com/Frank99/p/14004671.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!