python搜索文本内容_在python中如何在文本文件中搜索内容,并输出详细信息?_others_酷徒编程知识库...

完整的程序(第1部分有效):def AddSpaces(auth,numb):

print("Runs")

while len(auth) < numb:

auth = auth +""

return (auth)

menu = 1

while menu <= 2:

menu = int(input("1. Add a new bookn2. Search for a new book by a given authorn3. End"))

if menu ==1:

BOOKS = open("BOOKS.txt","a")

Author = str(input("Author:"))

if len(Author) < 16:

Author = AddSpaces(Author,16)

while len(Author) > 16:

Author = str(input("Author:"))

Author = AddSpaces(Author,16)

print(Author)

Book = str(input("Book:"))

if len(Book) < 20:

Book = AddSpaces(Book,20)

while len(Book) > 16:

Book = str(input("Book:"))

Book = AddSpaces(Book,20)

print(Book)

Price = str(input("Price:"))

while len(Price)>5:

Price = str(input("Price:"))

Copies = str(input("Copies:"))

while len(Copies)>2:

Copies = str(input("Copies:"))

line = Author + Book + Price + Copies +"n"

print(line)

BOOKS.write(line)

BOOKS.close()

elif menu == 2:

BOOKS = open("BOOKS.txt","r")

while True:

AuthorSearch = str(input("Author name for search:"))

if len(AuthorSearch) < 16:

AuthorSearch = AddSpaces(AuthorSearch,16)

while len(AuthorSearch) > 16:

AuthorSearch = str(input("Author name for search:"))

AuthorSearch = AddSpaces(AuthorSearch,16)

print(AuthorSearch)

n = BOOKS.read()

if n =="":

print("End of file, no books found")

break

if AuthorSearch == line[:16]:

print ("Author found.")

print ("Author name:") + (line[:16])

print ("Book name:") + (line[17:36])

print ("Price:") + (line[37:41])

print ("Copies:") + (line[42:43])

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值