import csv
word = input("please enter a word: ")
file = open('TEST.csv', 'r')
if column[0] in word or column[1] in word or column[2] in word or column[3] in word or column[4] in word:
print("The word you entered if in row " +str(count))
else:
print("The word you entered is NOT in row " +str(count))
我的代码不能正常工作,我希望代码允许我输入一个单词并查找它是否在csv文件中,然后它应该告诉我它是否存在或是否不是。
答案 0 :(得分:1)
import csv
word = input("please enter a word: ")
file = open('TEST.csv', 'r')
read = csv.reader(file)
count = 0
for column in read:
count = count + 1
if column[0] in word or column[1] in word or column[2] in word or column[3] in word or column[4] in word:
print("The word you entered if in row " +str(count))
else:
print("The word you entered is NOT in row " +str(count))
此代码无效,因为您有一个未定义的'count'。此外,您需要程序能够读取文件,而您错过了一点点代码。另外,因为您正在阅读的列中应该有
for column in read:
count = count + 1
不要忘记将'count'设置为变量本身。 祝你好运
答案 1 :(得分:1)
您的代码中存在一些基本问题。
count
变量column
列表我讨厌完全重写别人的代码,但你的代码需要一些帮助。此代码将实现您的目标:
word = input("Please enter a word: ")
delimiter = ","
with open("TEST.csv", 'r') as file:
content = file.read().replace('\n', '').split(delimiter)
if word in content:
print("The word you entered is in row {}".format(content.index(word)))
else:
print("The word you enterd is NOT in the file")
让我向您介绍此代码的功能和方式。