
import reimport xlrdimport xlsxwriter# Read exceldata = xlrd.open_workbook('D:\huangzheliang\Python\ Lunar calendar - Gregorian calendar conversion \ To be processed -- Instructions for filling in the pre-processing items of disaster relief time in the Qing Dynasty .xlsx')sheet = data.sheet_by_index(0)# write in excelworkbook = xlsxwriter.Workbook('D:\huangzheliang\Python\ Lunar calendar - Gregorian calendar conversion \ mark -- Instructions for filling in the pre-processing items of disaster relief time in the Qing Dynasty .xlsx')worksheet = workbook.add_worksheet('sheet1')bold_red = workbook.add_format({
'bold':True,'color':'red'})for row in range(3,sheet.nrows): # Target column target_Col = sheet.cell_value(row,4) # Processing column handle_Col = sheet.cell_value(row,3) # print(handle_Col) # Remove superfluous content 、 Split target column 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 = [] # Traverse the split list , And separate out the year 、 year 、 month 、 Japan and other information for time in target: # print(' Full date :' + time) # The year of withdrawal year_number = time[0:2] # Exclude empty list if time != '': # Locate the year # print(' primary :'+year_number) # print(handle_Col.find(year_number)) if handle_Col.find(year_number) != -1: # print(' new :'+year_number +'\n') final_year_number = year_number listBold.append(final_year_number) # Remove the remaining contents after the year year_and_rest = time[2:] # print(' After removing the year :' + year_and_rest) # Locate the remaining contents after removing the year number # 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 it cannot be located , Then continue to split if handle_Col.find(year_and_rest) == -1: # print(time) # Year of withdrawal # If time contain ' year ' if time.find(' year ') != -1: # print(time) yearList = year_and_rest.split(' year ') year = yearList[0] year2 = yearList[0] + ' year ' # 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) # What remains after the year of dismantlement 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 not, continue to split if handle_Col.find(month_and_rest) == -1: # print(month_and_rest) # Lending month monthList = month_and_rest.split(' month ') # 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) # The remaining contents after the month of withdrawal day_and_rest = monthList[1] if monthList[1] != '': # print(day_and_rest) # print(handle_Col.find(day_and_rest)) # Date of withdrawal dayList = day_and_rest.split(' Japan ') # print(time) # print(dayList) for i in dayList: if i != '': if handle_Col.find(i) != -1: final_day = i listBold.append(final_day) # If time It doesn't contain ' year ' if time.find(' year ') == -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) # What remains after the year of dismantlement 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) # List of words to be replaced 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(' original text :'+handle + '\n')
I want to replace all the list elements added with the specified content into the original text
In just four steps, you can understand the birth process of Python decorator
Python The decorators in are i
Poetry: Python dependency management and packaging tool [The best Python virtual environment Poetry] [Poetry, conda, vscode mixed use]
Poetry is a Python dependency