从文件中只删除一个字符串实例



我有一个像这样的文件:

1234:AnneShirly:anneshirley@seneca.ca:4:5[SRT111,OPS105,OPS110,SPR100,ENG100]
3217:Illyas:illay@seneca.ca:2:4[SRT211,OPS225,SPR200,ENG200]
1127:john Marcus:johnmarcus@seneca.ca:1:4[SRT111,OPS105,SPR100,ENG100]
0001:Amin Malik:amin_malik@seneca.ca:1:3[OPS105,SPR100,ENG100]

我希望能够要求用户输入(每行开头的学生编号),然后询问他们想要删除哪门课程(课程代码是列表)。因此,程序将从学生编号列表中删除该课程,而不删除该课程的其他实例。因为其他同学也上同样的课。

studentid = input("enter studentid")
course = input("enter the course to delete")
with open("studentDatabase.dat") as file:
f = file.readlines()
with open("studentDatabase.dat","w") as file:
for line in lines:
if line.find(course) == -1:
file.write(line)

这只是删除整行,但我只想删除课程

要做到这一点还有一段路要走。让我为你建议一个结构,也许你可以改进/增强,然后如果你在编辑上面的问题和/或评论这个答案时遇到困难,你可以重新发布。下面是我建议的一个框架:

  1. 将整个.dat文件中的一段代码读入内存。我建议将数据放入如下所示的字典中:

data = {1001: (name, email, <whatever the digits stand for>, [SRT111, OPS333, ...],
1044: ( ... )}

基本上是一个以ID为键的字典,其余的在元组或列表中。测试它,通过检查几个值来确保它正常工作。

  1. 创建一个小的"控制环";它使用你的输入语句,看看你是否能找到"记录"。从你的字典里。添加一些"if"做"某事"的逻辑如果没有找到ID,或者如果用户输入诸如"退出"之类的内容;退出/打破循环。测试它以确保它可以找到ID,然后再次测试它以查看它是否可以在包含数据的元组/列表中的列表中找到课程。你可能需要另一个"如果"。语句"做某事"如果课程不在数据元素中。测试它。

  2. 创建一个小的"辅助函数";它可以重写删除了课程的数据元素。建议的签名是:

    def remove_course(data_element, course):
    # make the new data element (name, ... , [reduced course list]
    return new_data_element
    

    测试它,确保它能工作。

  3. 把这些部分放在一起,你应该有成分来改变字典,通过使用循环和函数将新的数据元素放入字典,覆盖旧的。

  4. 编写一个小部件,从字典中完整地写入新的.dat文件。

编辑

你可以像这样从数据文件创建字典:

filename = 'student_data.dat'
data = {}  # an empty dictionary to stuff the results in
# use a context manager to handle opening/closing the file...
with open(filename, 'r') as src:
# loop through the lines
for line in src:
# strip any whitespace from the end and tokenize the line by ":"
tokens = line.strip().split(':')
# check it...  (remove later)
print(tokens)
# gather the pieces, make conversions as necessary...
stu_id = int(tokens[0])
name = tokens[1]
email = tokens[2]
some_number = int(tokens[3])
# splitting the number from the list of courses is a little complicated
# you *could* do this more elegantly with regex, but for your level, 
# here is a simple way to find the "chop points" and split this up...
last_blobs = tokens[4].split('[')
course_count = int(last_blobs[0])
course_list = last_blobs[1][:-1]  # everything except the last bracket
# split up the courses by comma
courses = course_list.split(',')
# now stuff that into the dictionary...
# a little sanity check:
if data.get(stu_id):
print(f'duplicate ID found: {stu_id}.  OVERWRITING')
data[stu_id] = (name,
email,
some_number,
course_count,
courses)
for key, value in data.items():
print(key, value)

我有东西给你。你要做的是先找到学生,然后删除课程:像这样。

studentid = input("enter studentid")
course = input("enter the course to delete")
with open("studentDatabase.dat") as file:
f = file.readlines()
with open("studentDatabase.dat","w") as file:
for line in lines:
if studentid in line:               # Check if it's the right sudent 
line = line.replace(course, "") # replace course with nothing
file.write(line)

您想要检查我们是否正在查看正确的学生,然后替换行,但不包含课程代码。希望对你有用。

相关内容

最新更新