首页 > 解决方案 > 在多个 excel 行中查找单词时的获取和错误

问题描述

有人可以帮我吗?我尝试将 global 添加到 thirdword_dp ,但它不起作用 第一次我只使用了 thirdword 但我有同样的错误 对于第一个 usr_choice 这个程序将只从 B 中查找单元格,如果选择了第二个 usr_choice 程序必须寻找两个B 中的一个单词和 C 中的一个单词。

我得到:> print('this is: ' + thirdword_dp) UnboundLocalError: local variable 'thirdword_dp' referenced before assignment

def finds_word_3():


    usr_choice = input('Would you like to search just in NAME or DP?\n Your choice: ')

    if usr_choice == '':
        print('Nothing Selected')
    elif usr_choice.lower() == 'name':
        # find first word
        for cell in range(1, 2000):
            data = sheet.cell(row=cell, column=2).value
            if data is not None:
                data = data.lower()
                data_no_pct = data.translate(str.maketrans('', '', string.punctuation))
                big_data1.append(data_no_pct)
        x1 = " ".join(big_data1)

        split_it1 = x1.split()

        Count1 = Counter(split_it1)

        most_occur1 = Count1.most_common(20)

        print(most_occur1)

        for word, cnt in most_occur1:
            answer = input('Is ' + word.upper() + ' a relevant word for you?\n Y or N:  ')
            if answer.upper() == 'Y':
                firstword = word
                break
        else:
            print('No word selected')

        print('this is: ' + firstword)

        # find 2nd word
        for cell in range(1, 2000):
            data = sheet.cell(row=cell, column=2).value
            if data is not None:
                data = data.lower()
                if firstword in data:
                    data_no_pct = data.translate(str.maketrans('', '', string.punctuation))
                    big_data2.append(data_no_pct)

        x1 = " ".join(big_data2)

        split_it1 = x1.split()

        Count1 = Counter(split_it1)

        most_occur1 = Count1.most_common(20)
        print(most_occur1)

        for word, cnt in most_occur1:
            if word != firstword:
                answer = input('Is ' + word.upper() + ' a relevant word for you?\n Y or N:  ')
                if answer.upper() == 'Y':
                    secondword = word
                    break
                else:
                    print('No word selected')

        print('this is: ' + secondword)

        # find 3rd word
        for cell in range(1, 2000):
            data = sheet.cell(row=cell, column=2).value
            if data is not None:
                data = data.lower()
                if firstword in data:
                    if secondword in data:
                        data_no_pct = data.translate(str.maketrans('', '', string.punctuation))
                        big_data3.append(data_no_pct)

        x1 = " ".join(big_data3)

        split_it1 = x1.split()

        Count1 = Counter(split_it1)

        most_occur1 = Count1.most_common(20)
        print(most_occur1)

        for word, cnt in most_occur1:
            if word != firstword:
                if word != secondword:
                    answer = input('Is ' + word.upper() + ' a relevant word for you?\n Y or N:  ')
                    if answer.upper() == 'Y':
                        thirdword = word
                        break
                    else:
                        print('No word selected')

        print('this is: ' + thirdword)

        print(
            '{AND\nAttributesContain[item_name:' + firstword + ']\nAttributesContain[item_name:' + secondword + ']\nAttributesContain[item_name:' + thirdword + ']\n}')


    elif usr_choice.lower() == 'dp':
        # find first word
        for cell in range(1, 2000):
            data = sheet.cell(row=cell, column=2).value
            if data is not None:
                data = data.lower()
                data_no_pct = data.translate(str.maketrans('', '', string.punctuation))
                big_data1.append(data_no_pct)
        x1 = " ".join(big_data1)

        split_it1 = x1.split()

        Count1 = Counter(split_it1)

        most_occur1 = Count1.most_common(20)

        print(most_occur1)

        for word, cnt in most_occur1:
            answer = input('Is ' + word.upper() + ' a relevant word for you?\n Y or N:  ')
            if answer.upper() == 'Y':
                firstword = word
                break
        else:
            print('No word selected')

        print('this is: ' + firstword)

        # find 2nd word
        for cell in range(1, 2000):
            data = sheet.cell(row=cell, column=2).value
            if data is not None:
                data = data.lower()
                if firstword in data:
                    data_no_pct = data.translate(str.maketrans('', '', string.punctuation))
                    big_data2.append(data_no_pct)

        x1 = " ".join(big_data2)

        split_it1 = x1.split()

        Count1 = Counter(split_it1)

        most_occur1 = Count1.most_common(20)
        print(most_occur1)

        for word, cnt in most_occur1:
            if word != firstword:
                answer = input('Is ' + word.upper() + ' a relevant word for you?\n Y or N:  ')
                if answer.upper() == 'Y':
                    secondword = word
                    break
                else:
                    print('No word selected')

        print('this is: ' + secondword)

        # find 3rd word
        for cell in range(1, 2000):
            data_dp = sheet.cell(row=cell, column=3).value
            if data_dp is not None:
                if data is not None:
                    data_dp = data_dp.lower()
                    if firstword in data:
                        if secondword in data:
                            data_no_pct = data_dp.translate(str.maketrans('', '', string.punctuation))
                            big_data3.append(data_no_pct)

        x1 = " ".join(big_data3)

        split_it1 = x1.split()

        Count1 = Counter(split_it1)

        most_occur1 = Count1.most_common(20)
        print(most_occur1)

        for word, cnt in most_occur1:
            if word != firstword:
                if word != secondword:
                    answer = input('Is ' + word.upper() + ' a relevant word for you?\n Y or N:  ')
                    if answer.upper() == 'Y':
                        thirdword_dp = word

                        break
                    else:
                        print('No word selected')

        print('this is: ' + thirdword_dp)

        print(
            '{AND\nAttributesContain[item_name:' + firstword + ']\nAttributesContain[item_name:' + secondword + ']\nAttributesContain[bullet_point:' + thirdword_dp + ']\n}')
    else:
        print('no go')

我收到的错误消息是:

Traceback (most recent call last):  
File "C:/Users/ancoman/PycharmProjects/pythonProject2/main.py", line 244, in <module> choice()  
File "C:/Users/ancoman/PycharmProjects/pythonProject2/main.py", line 23, in choice finds_word_3() 
File "C:/Users/ancoman/PycharmProjects/pythonProject2/main.py", line 229, in finds_word_3 dp() 
File "C:/Users/ancoman/PycharmProjects/pythonProject2/main.py", line 216, 
  in dp print('this is: ' + thirdword_dp) 
    UnboundLocalError: local variable 'thirdword_dp' referenced before assignment 

标签: pythonpython-3.xpython-2.7

解决方案


推荐阅读