Sinestro 2022-06-14 12:57 采纳率: 87.5%
浏览 75
已结题

python如何将列表的内容分别替换到字符串内

问题遇到的现象和发生背景

img


我希望将列表内的元素添加指定内容后全部替换到目标字符串的相应位置内,请问有什么方法吗?

问题相关代码,请勿粘贴截图

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')

运行结果及报错内容

img

我的解答思路和尝试过的方法
我想要达到的结果

我希望能将添加了指定内容的列表元素全部替换到原文内

  • 写回答

3条回答 默认 最新

  • 於黾 2022-06-14 13:18
    关注

    直接调用replace函数来替换
    原文=原文.replace('被替换的','替换的')
    不要循环一个一个的处理,麻烦不说,还没弄对,全是bug

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 6月23日
  • 已采纳回答 6月15日
  • 修改了问题 6月14日
  • 创建了问题 6月14日

悬赏问题

  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站