下面引用的是我的问题。我能够得到所示的输出,但我无法得到前面的数字,如问题中出现的单词出现在。你知道吗
Write a function definition for the following function and then invoke the function to test it works. The function:
search_file(filename, searchword)
should accept a filename (a file fields.txt has been provided on Moodle) and a user specified search word. It searches every line in the file for an occurrence of the word and if it exists it prints out the line preceded by the line number. Importantly it also writes the same output out to a file called
fieldsModified.txt
.
例如:
search_file("Fields-of-Athenry", "watched")
以上输出应采用以下格式:
9 - Where once we watched the small free birds fly.
21 - Where once we watched the small free birds fly.
26 - She watched the last star falling
33 - Where once we watched the small free birds fly."
试试这个:
有多种方法可以做到这一点;我认为这是最直接的:
这是否适合你的需要取决于你需要搜索的文件有多大,你是否关心大小写等。我不确定这是否直接解决了你的问题,所以如果我错过了你想问的问题,请这么说。。。你知道吗
相关问题 更多 >
编程相关推荐