问题遇到的现象和发生背景
我希望将列表内的元素添加指定内容后全部替换到目标字符串的相应位置内,请问有什么方法吗?
问题相关代码,请勿粘贴截图
import re
import xlrd
import xlsxwriter
# 读取excel
data = xlrd.open_workbook('D:\huangzheliang\Python\农历-公历转化\待处理--清代灾赈时间预处理项填写说明.xlsx')
sheet = data.sheet_by_index(0)
# 写入excel
workbook = 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')
运行结果及报错内容
我的解答思路和尝试过的方法
我想要达到的结果
我希望能将添加了指定内容的列表元素全部替换到原文内