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

时间:2018-11-02 07:42:12

标签: 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)

print(arr_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']

上面的代码在此方法中-下面的convert_text(choices):

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)

1 个答案:

答案 0 :(得分:1)

        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"