gpt4 book ai didi

Python 在第一行以外的任何行都找不到短语(新)

转载 作者:行者123 更新时间:2023-11-28 17:33:20 31 4
gpt4 key购买 nike

和我的last question ,我的程序无法检测到一个短语并将其与第一行以外的任何行匹配。但是,我已经解决并回答了。但现在我需要一个新的 def函数,它删除某个(给定 refName )联系人及其下方与该联系人相关的 4 行,但是,我遇到了与 readfile 相同的问题功能;它只检测第一行,而不检测其他任何内容。

读取文件

def readFile(self):

lookup = input("Type in a contact REFERENCE name.\n")

with open('contacts.txt') as myFile:
my_file_iter = iter(myFile)
for num, line in enumerate(my_file_iter, 1):
if lookup.upper() in line:

print(line)
print(next(my_file_iter))
print(next(my_file_iter))
print(next(my_file_iter))
print(next(my_file_iter))
break
else:
print("Contact not found.")
self.managerMenu()

删除联系人

def delContact(self):

lookup = input("Type in a contact REFERENCE name.\n")

with open('contacts.txt') as myFile:
my_file_iter = iter(myFile)
for num, line in enumerate(my_file_iter, 1):
if lookup.upper() != "YOU":
if lookup.upper() in line:
# read a list of lines into data
data = myFile.readlines()
print("Deleting contact ", data[num - 1][9:])
# now change the lines, note that you have to add a newline
data[num - 1] = ''
data[num] = ''
data[num + 1] = ''
data[num + 2] = ''
data[num + 3] = ''
# and write everything back
myFile.writelines( data )

break
else:
print("Contact not found.")
break

else:
print("Cannot delete yourself!")
self.delContact()

self.managerMenu()

联系人.TXT

Contact: YOU
First Name: FELIX
Last Name: MARTIN
Number: (555)-555-5554
Address: 3550 VISTA PARK DRIVE
Contact: FRIEND
First Name: DAVID
Last Name: BRENNEMAN
Number: (555)-555-5555
Address: 123 SESAME STREET
Contact: MOM
First Name: SANDY
Last Name: MARTIN
Number: (555)-555-5556
Address: 3550 VISTA PARK DRIVE

当我运行我的程序时,readfile 和 delcontact 都会发生这种情况。

(请记住,代码摘录和程序的某些部分是整个项目的一部分,此处未提及,以消除任何混淆。整个文件将在末尾标记。 )

readFile

Contact Manager v1.4 - Felix Martin
Loading... Loaded!
Welcome, FELIX

Available Commands: Info, ReadFile, DeleteContact, EditContact, AddContact, quit()
readfile
Type in a contact REFERENCE name.
you
Contact: YOU

First Name: FELIX

Last Name: MARTIN

Number: (555)-555-5554

Address: 3550 VISTA PARK DRIVE

Available Commands: Info, ReadFile, DeleteContact, EditContact, AddContact, quit()
readfile
Type in a contact REFERENCE name.
friend
Contact: FRIEND

First Name: DAVID

Last Name: BRENNEMAN

Number: (555)-555-5555

Address: 123 SESAME STREET

Available Commands: Info, ReadFile, DeleteContact, EditContact, AddContact, quit()

删除联系人

Contact Manager v1.4 - Felix Martin
Loading... Loaded!
Welcome, FELIX

Available Commands: Info, ReadFile, DeleteContact, EditContact, AddContact, quit()
deletecontact
Type in a contact REFERENCE name.
you
Cannot delete yourself!
Type in a contact REFERENCE name.
friend
Contact not found.
Available Commands: Info, ReadFile, DeleteContact, EditContact, AddContact, quit()

有什么想法吗?我要求在提交答案时,我更喜欢按照我展示的方式完成,而不是使用 RegEx,如果可能的话,在 Python 3.4 中完成。

Contact Manager File

最佳答案

听起来您需要一套更全面的工具来处理您的数据。如果您打算学习一个(非常有值(value)的)新模块,我强烈建议您将这些数据迁移到某种类型的数据库中。 sqlite3是 Python 的流行数据库,从 Python 2.5 开始包含在标准库中。

就是说,如果您致力于将其制作为平面文件,听起来您真的真的需要一个函数来为自己构建一个数据的工作副本,以更有用的方式(如字典)进行。我建议在应用程序运行时立即运行它,并引用数据库以进行任何进一步的调用。

def build_db(path):
db = {}
with open(path) as f:
for line in f:
category, value = map(str.strip, line.split(":"))
if category == "CONTACT":
cur_contact = value
db[value] = {}
else:
db.get(cur_contact, {})[category] = value
# builds a dictionary of dictionaries that looks like:
# # {"YOU": {"First Name": "FELIX", "Last Name": "MARTIN", ...}, ...}
return db

然后在你的应用程序的入口点你可以写:

db = build_db("path/to/your/file.txt")

从那时起,任何读取或写入都是通过您设计的 API 访问 db 对象来处理的。

def read_contact(db, contact_name):
formatting = """\
CONTACT: {contact}
First Name: {First Name}
Last Name: {Last Name}
Number: {Number}
Address: {Address}
"""
contact_info = db.get(contact_name)
if contact_info is None:
raise KeyError("No such contact: {}".format(contact_name))
contact_info['contact'] = contact_name
return formatting.format(**contact_info)

def write_db_to_file(db, out_path):
with open(out_path, 'w') as outf:
for contact_name in db:
outf.write(read_contact(db, contact_name))

def remove_user_from_db(db, contact_name):
try:
del db[contact_name]
except KeyError:
raise KeyError("No such contact: {}".format(contact_name))

关于Python 在第一行以外的任何行都找不到短语(新),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32743421/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com