标签: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