python - 在多个 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
解决方案
推荐阅读
- php - 如何在电子邮件正文中获取多字段表单数据
- javascript - 删除本地存储数组中的特定元素
- node.js - 在 fetch 中自动填充 cookie(如浏览器)
- javascript - 初学者 javascripter 在函数中调用函数时遇到问题
- numpy - 循环并将 3 个不同的 numpy 数组乘以随机百分比,加起来为 100%
- javascript - 需要建议(指导)
- python - 在 Python 中使用“i 不等于 j”循环重新计算对称矩阵的元素
- azure - .Net Core 5.0 - Sql Azure + Always Encrypted + Managed Identity
- java - 一种可以采用 String 格式的整数数组并返回这些数字的平均值的方法
- swift - RealityKit – 在 3d 空间中的两点之间创建线