我不知道该怎么做才能从文件中删除这本书。
我的代码接受用户的输入,以确定他们要从列表中删除哪本书。然后打开文本文件并阅读。
我使用.strip()
进行了研究,但似乎不起作用,并且我对.strip()
的功能不熟悉
def delete_book():
book_to_delete = input('Please input the name of the book that will be removed from the library: ')
with open("listOfBook.txt", "r") as list_of_books:
books = list_of_books.readlines()
with open('listOfBook.txt', 'w') as list_of_books:
for book in books:
if book.strip('\n') != book_to_delete:
list_of_books.write(book)
print(book_to_delete, 'had been removed from the library data base')
input('Please press enter to go back to staff menu')
这是我的文本文件(listOfBook.txt
):
The Great Gatsby
To Kill a Mockingbird
Harry Potter and the Sorcerer's Stone
1984
答案 0 :(得分:2)
在可以正确删除行中的\n
个字符的地方,您的代码在大多数情况下都是正确的。
您还会有一个缩进问题,告诉用户书已被删除,但这可能与您在此处将代码发布到问题中有关。它不会影响您的代码如何测试每一行。
但是,从每个\n
值中删除book
个字符并不意味着从文件中读取的字符串将与用户键入的字符串匹配:
您的行可能会有多余的空格。在文本编辑器中打开书本文件时,这很难看到,但是您可以通过使用更多的Python代码进行测试。
尝试使用repr()
function或ascii()
function来打印行,这将使它们看起来像是Python字符串文字。如果仅使用英文文本,ascii
和repr()
之间的区别并不重要;重要的是,您可以通过一种更容易发现空格之类的方式来查看字符串值。
只需在您的循环中添加一个print("The value of book is:", ascii(book))
甚至是print("The value of book_to_delete is:", ascii(book_to_delete))
:
with open('listOfBook.txt', 'w') as list_of_books:
for book in books:
print("The value of book is:", ascii(book))
print("The value of book_to_delete is:", ascii(book_to_delete))
if book.strip('\n') != book_to_delete:
您可以从"\n"
删除str.strip()
参数,并从头到尾删除所有空格字符来解决此问题:
if book.strip() != book_to_delete.strip():
您可以在Python交互式会话中使用该功能:
>>> book = "The Great Gatsby \n"
>>> book
'The Great Gatsby \n'
>>> print(book.strip("\n"))
The Great Gatsby
>>> print(ascii(book.strip("\n")))
'The Great Gatsby '
>>> print(ascii(book.strip()))
'The Great Gatsby'
请注意,print(book.strip("\n"))
并没有真正向您显示那里还有多余的空格,但是print(ascii(book.strip("\n")))
通过引用'...'
的位置显示了字符串更长。最后,不使用任何参数的str.strip()
会删除这些多余的空格。
此外,请注意,用户也可能会添加额外的空格,也请删除这些空格。
用户可能使用大小写字符的不同组合。您可以在两个值上使用str.casefold()
function`来确保忽略大小写差异:
if book.strip().casefold() != book_to_delete.casefold():
您发布的代码存在缩进问题。线
print(book_to_delete, 'had been removed from the library data base')
input('Please press enter to go back to staff menu')
当前缩进到if book.strip('\n') != book_to_delete:
测试块之内,因此每次测试文件中的book
值并发现它是另一本书时,便会执行它们
您希望删除足够的缩进,以便仅在def delete_book():
级别之后缩进一次,因此仍然是函数的一部分,而不是其他任何块的一部分:
def delete_book():
book_to_delete = input('Please input the name of the book that will be removed from the library: ')
with open("listOfBook.txt", "r") as list_of_books:
books = list_of_books.readlines()
with open('listOfBook.txt', 'w') as list_of_books:
for book in books:
if book.strip() != book_to_delete.strip():
list_of_books.write(book)
print(book_to_delete, 'had been removed from the library data base')
input('Please press enter to go back to staff menu')
这样,只有在将与book_to_delete
不匹配的所有行都写入文件并且文件已关闭之后,才会执行该文件。请注意,在上面的示例中,我还将.strip("\n")
更改为.strip()
,并且还向用户输入添加了额外的strip()
调用。.
答案 1 :(得分:0)
我对代码进行了微小的更改,希望能够解释您遇到的一些问题(某些问题可能只是问题的格式设置)
def delete_book():
book_to_delete = input('Please input the name of the book that will be removed from the library: ')
with open("listOfBook.txt", "r") as list_of_books:
books = list_of_books.readlines()
with open('listOfBook.txt', 'w') as list_of_books:
for book in books:
if book.strip('\n').strip() != book_to_delete: # Code Changes
list_of_books.write(book)
else: # Code Changes
print(book_to_delete, 'had been removed from the library data base')
input('Please press enter to go back to staff menu')
首先,我在第7行的检查中添加了第二个.strip()
。这将删除所有前导或尾随空格(这将导致不匹配)
此外,我还通过“当书被删除时”的报告重新组织了一些逻辑。
希望这能如您所愿。
答案 2 :(得分:0)
book_to_delete = input('Please input the name of the book that will be removed from
the library: ')
with open("listOfBook.txt", "r") as list_of_books:
books = [line.strip() for line in list_of_books]
found_book = False
for i in range(len(books)):
if books[i] == book_to_delete:
found_book = True
break
if found_book == False:
print("Book not found in the list")
else:
books.remove(book_to_delete)
open('listOfBook.txt', 'w').close()
with open('listOfBook.txt', 'w') as list_of_books:
for bookTitle in books:
list_of_books.write('%s\n' % bookTitle)
答案 3 :(得分:-1)
像这样更改代码
def delete_book():
book_to_delete = input('Please input the name of the book that will be removed
from the library: ')
with open("listOfBook.txt", "r") as list_of_books:
books = list_of_books.readlines()
with open('listOfBook.txt', 'w') as list_of_books:
for book in books:
if book.strip('\n') != book_to_delete:
list_of_books.write(book)
print(book_to_delete, 'had been removed from the library data base')
input('Please press enter to go back to staff menu')