def make(): concentration_path_dict = {} compared_path_list2 = [] ck = spectrum(chose_entry.get()) file_path = folder_entry.get() if file_path[-1] != '/' and file_path[-1] != '\\': file_path += '/' compared_path_list = get_all_file_path(file_path) if chose_entry.get() in compared_path_list: compared_path_list.remove(chose_entry.get()) for each_compared_path in compared_path_list: each_compared_path = each_compared_path.decode('gbk') # print(each_compared_path) # print(type(each_compared_path)) print(each_compared_path.split('/')) concentration_path_dict[re.search(r'(?P<concentration2>\d.\d\d|\d\d.\d|\d{3})', each_compared_path.split('/')[-1]).group('concentration2')] = each_compared_path for path_dict in sorted(concentration_path_dict, key=lambda v: [int(i) for i in v.rstrip('@').split('.')]): compared_path_list2.append(concentration_path_dict[path_dict]) standard_peaks_locs = peak_entry.get().split(';') # 下面可以加上排序 if standard_peaks_locs[-1] == '': standard_peaks_locs.remove(standard_peaks_locs[-1]) make_path = make_entry.get() if make_path[-1] != '/' and make_path[-1] != '\\': make_path += '/' try: new_file = open(make_path+name_entry.get()+'.csv', 'a') for loc in standard_peaks_locs: new_file.write(',,'+loc) new_file.write('\n') new_file.write(ck.concentration) for loc in standard_peaks_locs: new_file.write(',,'+ck[loc]) new_file.write('\n') for path in compared_path_list2: compared_peaks_locs_float = [] spec = spectrum(path) final_r = spec.concentration for loc in standard_peaks_locs: if loc in spec.locs: final_r += ',,'+spec[loc] else: standard_loc_float = float(loc) for compared_loc_str in spec.locs: compared_peaks_locs_float.append(float(compared_loc_str)) close_peak_loc = find_close(standard_loc_float, compared_peaks_locs_float) final_r += ",%s,%s" % (close_peak_loc, spec['{:.2f}'.format(Decimal(close_peak_loc))]) new_file.write(final_r+'\n') new_file.write('\n') tkMessageBox.showinfo("已完成", "已完成") except: tkMessageBox.showwarning("Failed", "未能生成文件") finally: new_file.close()
def make(): concentration_path_dict = {} compared_path_list2 = [] ck = spectrum(chose_entry.get()) file_path = folder_entry.get() if file_path[-1] != '/' and file_path[-1] != '\\': file_path += '/' compared_path_list = glob.glob(file_path+'*.txt') compared_path_list = [path.decode('utf-8').replace('\\', '/') for path in compared_path_list] if chose_entry.get() in compared_path_list: compared_path_list.remove(chose_entry.get()) for each_compared_path in compared_path_list: each_compared_path_last = each_compared_path.split('/')[-1] concentration_path_dict[re.search(r'(?P<concentration2>\b\d\.\d{1,3}\b|\b\d{3,4}\b|\b\d{2}\.\d{0,2}\b)', each_compared_path_last).group('concentration2')] = each_compared_path for path_dict in sorted(concentration_path_dict, key=lambda v: [int(i) for i in v.rstrip('@').split('.')]): compared_path_list2.append(concentration_path_dict[path_dict]) standard_peaks_locs = peak_entry.get().split(';') # 下面可以加上排序 if standard_peaks_locs[-1] == '': standard_peaks_locs.remove(standard_peaks_locs[-1]) standard_peaks_locs = sorted(standard_peaks_locs, key=lambda v: [int(i) for i in v.rstrip('@').split('.')], reverse=True) make_path = make_entry.get() if make_path[-1] != '/' and make_path[-1] != '\\': make_path += '/' ck_list = [] ck_list.append('ck') for loc in standard_peaks_locs: ck_list.append('') ck_list.append(ck[loc]) ck_list.append('') all_fin = [] all_fin.append(ck_list) for path in compared_path_list2: compared_peaks_locs_float = [] spec = spectrum(path) current_list = [] current_list.append(spec.concentration) for loc in standard_peaks_locs: if loc in spec.locs: current_list.append('') current_list.append(spec[loc]) percentage_shift = str((float(spec[loc])-float(ck[loc]))/float(ck[loc])*100) percentage_shift = '{:.2f}'.format(Decimal(percentage_shift))+'%' current_list.append(percentage_shift) else: standard_loc_float = float(loc) for compared_loc_str in spec.locs: compared_peaks_locs_float.append(float(compared_loc_str)) close_peak_loc = find_close(standard_loc_float, compared_peaks_locs_float) current_list.append(close_peak_loc) current_list.append(spec['{:.2f}'.format(Decimal(close_peak_loc))]) close_peak_loc = '{:.2f}'.format(Decimal(close_peak_loc)) percentage_shift = str((float(spec[close_peak_loc])-float(ck[loc]))/float(ck[loc])*100) percentage_shift = '{:.2f}'.format(Decimal(percentage_shift))+'%' current_list.append(percentage_shift) all_fin.append(current_list) standard_peaks_locs_insert = standard_peaks_locs standard_peaks_locs_insert.insert(0, '浓度') standard_peaks_locs_insert = (sum([[i, ''] for i in standard_peaks_locs_insert], [])[:-1]) standard_peaks_locs_insert2 = [] i = 2 for insert in standard_peaks_locs_insert: standard_peaks_locs_insert2.append(insert) if i != 2 and i%2 == 0: standard_peaks_locs_insert2.append('比率') i = i+1 data = tablib.Dataset(*all_fin, headers=standard_peaks_locs_insert2, title='sheet1') try: with open(make_path+name_entry.get()+'.xls', 'ab') as fin_file: fin_file.write(data.xls) tkMessageBox.showinfo("已完成", "已完成") except: tkMessageBox.showwarning("Failed", "未能生成文件")