如何在for循环中将多个旧字符串替换为新字符串?

2024-05-16 02:39:43 发布

您现在位置:Python中文网/ 问答频道 /正文

请帮我看一下下面的代码:

 elif admission_name in getFilenameOnly:
                categoryType = ('_'.join(getFilenameOnly.split('_')[1:3]))
                categoryType = categoryType.replace("_", "")
                categoryType = categoryType.replace("Cat", "")
                categoryType = categoryType.replace(" ", "")
                arr_cat=[]
                arr_cat += [categoryType]
                print(arr_cat)

打印输出(arr\u cat)为:

['5']
['5']
['3']
['4']
['5']
['7']
['6']
['6']
['6']
['6']
['7']
['7']
['7']
['3']
['6']
['7']
['6']
['4']
['7']
['4']
['5']
['5']
['5']
['6']
['4']
['7']
['7']
['8']
['5']
['5']
['6']
['8']
['7']
['4']
['7']
['4']
['6']

我想做的是将“3”、“4”、“5”、“8”替换为“其他” 期望输出为:

    ['others']
    ['others']
    ['others']
    ['others']
    ['others']
    ['others']
    ['6']
    ['6']
    ['6']
    ['6']
    ['others']
    ['others']
    ['others']
    ['others']
    ['6']
    ['others']
    ['6']
    [others']
    ['others']
    ['others']
    ['others']
    ['others']
    ['others']
    ['6']
    ['others']
    ['others']
    ['others']
    ['others']
    ['others']
    ['others']
    ['6']
    ['others']
    ['others']
    ['others']
    ['others']
    ['others']
    ['6']

上面的代码在此方法中-转换下面的文本(选项):

def convert_txt(choices):

    root_dir, ednotes_name, admission_name, discharge_name, output, root_dir2, convert_docx, output_cat67 = read_config()

    if choices == 1:

        # open new file to write string data textfile
        text_file = open(output, 'w', encoding='utf-8')
        text_file.write("cat_id|content\n")

        # open new file to write string data -> "cat_id|content" for output_cat67
        text_file = open(output_cat67, 'w', encoding='utf-8')
        text_file.write("cat_id|content\n")

        for filename in os.listdir(root_dir):
            source_directory = root_dir + '/' + filename
            getFilenameOnly = os.path.basename(source_directory)
            whole_string = ""
            document = ""

            document += docx2txt.process(source_directory)

            if ednotes_name in getFilenameOnly:
                arr = ednotes_extractor.get_ednotes(source_directory)
                list2str = str(arr)
                c = cleanString(newstring=list2str)
                ednotes_arr = []
                ednotes_arr += [c]
                # open existing file to append the items in the array to the 
                  previously written textfile
                text_file = open(output, 'a', encoding='utf-8')
                for item in ednotes_arr:
                    text_file.write("%s\n" % item)

            ***Please help me look at the codes here***

            elif admission_name in getFilenameOnly:
                categoryType = ('_'.join(getFilenameOnly.split('_')[1:3]))
                categoryType = categoryType.replace("_", "")
                categoryType = categoryType.replace("Cat", "")
                categoryType = categoryType.replace(" ", "")
                arr_cat=[]
                arr_cat += [categoryType]
                print(arr_cat)

                for word in document.split():
                    whole_string += word + " "

                whole_string = delete_phrase(whole_string)
                whole_string = delete_header(whole_string)

                # currently testing for cat 6-7
                text_file = open(output, "a", encoding='utf-8')
                admission_output = categoryType + '|' + whole_string
                admission_arr = []
                admission_arr += [admission_output]
                for item in admission_arr:
                    text_file.write("%s\n" % item)

Tags: textnameinoutputstringopencatfile
1条回答
网友
1楼 · 发布于 2024-05-16 02:39:43
        elif admission_name in getFilenameOnly:
            categoryType = ('_'.join(getFilenameOnly.split('_')[1:3]))
            categoryType = categoryType.replace("_", "")
            categoryType = categoryType.replace("Cat", "")
            categoryType = categoryType.replace(" ", "")

            if categoryType in ["3","4","5","8"]:
                categoryType = "other"

相关问题 更多 >