import reimport xlrdimport xlsxwriter# 讀取exceldata = xlrd.open_workbook('D:\huangzheliang\Python\農歷-公歷轉化\待處理--清代災赈時間預處理項填寫說明.xlsx')sheet = data.sheet_by_index(0)# 寫入excelworkbook = xlsxwriter.Workbook('D:\huangzheliang\Python\農歷-公歷轉化\標注--清代災赈時間預處理項填寫說明.xlsx')worksheet = workbook.add_worksheet('sheet1')bold_red = workbook.add_format({
'bold':True,'color':'red'})for row in range(3,sheet.nrows): # 目標列 target_Col = sheet.cell_value(row,4) # 處理列 handle_Col = sheet.cell_value(row,3) # print(handle_Col) # 清除多余內容、拆分目標列 target1 = target_Col.replace('【', '') target2 = target1.replace('】', '') target3 = target2.replace('(+)', '') target4 = target3.replace('(-)', '') target5 = re.sub('[A-Z]','',target4) target = target5.split(',') # print(target) listBold = [] #遍歷拆分後的列表,並分別拆出年號、年、月、日等信息 for time in target: # print('完整日期:' + time) # 拆出年號 year_number = time[0:2] # 排除空列表 if time != '': # 定位年號 # print('原:'+year_number) # print(handle_Col.find(year_number)) if handle_Col.find(year_number) != -1: # print('新:'+year_number +'\n') final_year_number = year_number listBold.append(final_year_number) # 拆除年號後剩余內容 year_and_rest = time[2:] # print('拆除年號後:' + year_and_rest) # 定位拆除年號後剩余內容 # print(handle_Col.find(year_and_rest)) if handle_Col.find(year_and_rest) != -1: # print(year_and_rest) # print(handle_Col.find(year_and_rest)) final_year_and_rest = year_and_rest listBold.append(final_year_and_rest) # 若定位不到,則繼續拆分 if handle_Col.find(year_and_rest) == -1: # print(time) # 拆出年份 # 如果time含有'年' if time.find('年') != -1: # print(time) yearList = year_and_rest.split('年') year = yearList[0] year2 = yearList[0] + '年' # print(year) # print(handle_Col.find(year)) if handle_Col.find(year2) != -1: final_year = year2 listBold.append(final_year) elif handle_Col.find(year) != -1: final_year = year listBold.append(final_year) # 拆出年份後剩余內容 month_and_rest = yearList[1] # print(month_and_rest) # print(handle_Col.find(month_and_rest)) if handle_Col.find(month_and_rest) != -1: final_month_and_rest = month_and_rest listBold.append(final_month_and_rest) # 若匹配不到則繼續拆分 if handle_Col.find(month_and_rest) == -1: # print(month_and_rest) # 拆出月份 monthList = month_and_rest.split('月') # print(monthList) month = monthList[0] # print(month) # print(handle_Col.find(month)) if handle_Col.find(month) == -1: final_month = month listBold.append(final_month) # 拆出月份後剩余內容 day_and_rest = monthList[1] if monthList[1] != '': # print(day_and_rest) # print(handle_Col.find(day_and_rest)) # 拆出日期 dayList = day_and_rest.split('日') # print(time) # print(dayList) for i in dayList: if i != '': if handle_Col.find(i) != -1: final_day = i listBold.append(final_day) # 如果time不包含'年' if time.find('年') == -1: # print(year_and_rest) if len(year_and_rest) != 1: year = year_and_rest[0:2] # print(year) # print(handle_Col.find(year)) if handle_Col.find(year) != -1: final_year = year listBold.append(final_year) # 拆出年份後剩余內容 month_and_rest = year_and_rest[2:] # print(month_and_rest) # print(handle_Col.find(month_and_rest)) if handle_Col.find(month_and_rest) != -1: final_month_and_rest = month_and_rest listBold.append(final_month_and_rest) # print(listBold) result_list = [] for ele in listBold: if ele not in result_list and ele != '': result_list.append(ele) # 需替換的文字列表 for i in range(len(result_list)): if len(result_list)!=0: if result_list[i] is not result_list[-1]: if result_list[i].find(result_list[i+1]) != -1: result_list.pop(i+1) break for i in range(len(result_list)): if len(result_list)!=0: if result_list[i] is not result_list[-1]: if result_list[i].find(result_list[i+1]) != -1: result_list.pop(i+1) break print(result_list) result_str = ''.join(result_list) handle1 = handle_Col.replace('【', '') handle = handle1.replace('】', '') if len(result_list) != 0: for ele in result_list: if ele != '': replace_ele = ',bold_red,'+ ele + ',' print(replace_ele) target_handle = re.sub(ele,replace_ele,handle) # print(target_handle +'\n') print('原文:'+handle + '\n')
我希望能將添加了指定內容的列表元素全部替換到原文內