擅长:python、mysql、java
<p>如果您的文本文件用'\n'分隔,则您可以使用:</p>
<pre><code>THE_LINE = "Harsh Patel"
file = open("filename.txt", "r")
# read the text as a list of lines:
text = file.readlines()
file.close()
# use filter, to filter elements from a list, use lambda to create a mini function:
new_text = filter(lambda element: THE_LINE not in element, text)
file = open("filename.txt", "w")
# use join to make a string out of a list in the format you choose, the string there is empty because we have the '\n' in the list already:
file.write("".join(new_text))
file.close()
</code></pre>
<p>要重新检查您的代码,您可以使用此代码将重写为文件:</p>
<pre><code>a = "Harsh Patel 20.0 60.0\nPeter Piper 15.5 40.0\nManish Mannon 27.5 38.5\nLuke Papa 25 50"
file = open("filename.txt", "w")
file.write(a)
file.close()
</code></pre>