我有一个包含2列的excel文件,如下所示。我想检查列名“CODE1”中的单元格内容是否存在于从屏幕输入的字符串中。然后在屏幕上返回结果是列名称“结果”
import pandas as pd
import getpass
import random
path_to_csv_file = 'C:\\names.xlsx'
code_names_dataframe = pd.read_excel(path_to_csv_file)
code_names_dictionary = code_names_dataframe.to_dict(orient='records')
user_response = input()
user_response=user_response.lower()
while True:
name = None
username = user_response
for code_name in code_names_dictionary:
if username.contains(code_name['CODE1']).any():
name = code_name['RESULT']
if name is not None:
break
else:
print('Not Correct')
break
print(name)
Excel 格式如下:
CODE1 RESULT
excel OK. Excel
apple OK. Apple
我想当用户在屏幕上输入字符串为“我想收到一个苹果”...然后结果将在屏幕上返回是“OK. 苹果”
当我运行代码时,它显示错误如下:
Traceback (most recent call last):
File "C:\Users\Administrator\Desktop\Chatbot - ReadData\ExcelCSV.py", line 58, in <module>
mainmenu()
File "C:\Users\Administrator\Desktop\Chatbot - ReadData\ExcelCSV.py", line 49, in mainmenu
if username.contains(code_name['CODE1']).any():
AttributeError: 'str' object has no attribute 'contains'
慕容森
相关分类