更新:我目前的问题是如何让我的代码从一开始就从每个新的搜索短语开始读取到EOF。AttributeError:'str'对象没有属性'readline'
这是我正在做的工作,目前正在进行中。请注意,这是一个使用Python的初学者编程课程。
jargon = open("jargonFile.txt","r")
searchPhrase = raw_input("Enter the search phrase: ")
while searchPhrase != "":
result = jargon.readline().find(searchPhrase)
if result == -1:
print "Cannot find this term."
else:
print result
searchPhrase = raw_input("Enter the search phrase: ")
jargon.close()
的任务是把用户的searchPhrase,发现它在一个文件中(jargonFile.txt),然后把它打印出结果(这是它发生的行和字符occurence)。我将使用计数器来查找出现的行号,但我会稍后再执行。现在我的问题是我得到的错误。我无法找到一种方法来搜索整个文件。
样品运行:
Enter the search phrase: dog
16
Enter the search phrase: hack
Cannot find this term.
Enter the search phrase:
“狗”是在第一行中找到但是它也在(多次作为字符串)的jargonFile的其它系中发现但它仅示出了在第一次出现第一行。字符串破解在jargonFile中找到了很多次,但我的代码设置为只搜索第一行。我该如何解决这个问题?
如果这不够清楚,我可以在需要的时候发布作业。
我修改了代码。样品运行中的错误仍然存在。 – 2009-02-27 22:24:10