繁体   English   中英

从文件中读取元组列表/将数据保存到文件

[英]Reading list of tuples from a file/saving data to a file

我有一个给定学生学期的课程列表。 我想将此类列表保存到名为 classes.txt 的 txt 文件中。 这充当一种“保存”文件。 再次打开时,程序将读取文件并了解学生正在上什么课。

我使用以下代码执行此操作:

def write_out():
    f_out = open('classes.txt', 'w')

# classes is a list of classes that the student has created
# all the classes are objects which have a few parameters listed below
    for course in classes:
        f_out.write(str(Course.course_name(course)) + ',' +   str(Course.credits(course)) + ',' + str() + ',' + str(Course.assignments(course)) + '\n')

写入文件的内容如下所示:

Bio,4,[('exam', 100)],[]
calc,4,[('exam', 50)],[]

Course对象由几个参数定义:

  1. 一个名字(即“生物”)
  2. 学分数
  3. 课程评分类别的元组列表(类别名称,然后是权重)
  4. 作业清单*

*作业列表是由名称、类别和等级定义的作业对象列表

我选择将类别保留为元组,因为它看起来更容易。


当我在程序启动时尝试读取文件时出现了我的问题。 虽然将分配和类别写入列表相对简单,但将其读回变得更加困难,因为当我将元组列表转换回例如类别时,似乎有双引号。

我的问题是:如何更轻松地将 txt 文件中的元组和列表读入我的程序?

我从阅读这篇文章开始,但我走到了死胡同,因为这篇文章更多地是关于将元组专门保存到文件中,而我有很多参数需要在启动程序时转换为对象。

我想扩大这篇文章,以更多地了解一般的保存文件,以更好地理解我应该如何解决这个问题。

阅读文件后,我根据保存文件中的参数创建一个新的Course对象,并将其添加到名为“courses”的列表中,稍后可以访问该列表。

旁注:我什至不确定这种方法是否是最有效的做事方式,所以如果有人对我如何更有效地将课程保存到文件有更好的想法,那么我 100% 对它持开放态度。 我计划将大量课程和作业写入此文件,因此正确写入非常重要

感谢您的帮助,这是我在学习路径的早期开始的一个项目,所以我只想重新访问它以了解如何将数据保存到文件:p

我将为您提供一个 DIY 解决方案,没有外部库,让您了解工作流程。 评论中给出的建议更多的是好的,但下划线的原则应该是“相似的”。

此处考虑性能、安全性,将其视为您学习路径的编码更新(或者我希望如此)。

要求

  1. 在写入文件时使用唯一的分隔符,当您稍后阅读它时会更容易。 我选择; 但可以自由更改它( ,与列表发生冲突,& co)
  2. 在编写字符串对象时,“double double”引用它们,以便在文件中它们将被双引号包围(需要eval请参阅下一步),因此字符串应采用'"Bio"'形式
  3. 使用eval将字符串“复活”为对象
  4. 在课程类中添加写/读方法。 特别地,读取器是一个类方法,因为它返回一个类实例
class Course:
    
    SEP = ';' # should be a unique character(s) 
    
    def __init__(self, name, credits_, grading_cats, assignments):
        self.name, self.credits, self.grading_cats, self.assignments = self.data = name, credits_, grading_cats, assignments

    def __str__(self): # not needed but useful to understand the difference with "write_file_formatter", see 2.
        return f'{self.SEP}'.join( str(i) for i in self.data)

    def write_file_formatter(self):
        return f'{self.SEP}'.join( f'"{i}"' if isinstance(i, str) else str(i) for i in self.data)

    @classmethod
    def read_file_formatter(cls, course_string):
        return cls(*map(eval, course_string.split(cls.SEP)))

# the courses        
c1 = Course('Bio', 4, [('exam', 100)], [])
c2 = Course('cal', 4, [('exam', 50)], [])

print(c1.write_file_formatter())
# "Bio";4;[('exam', 100)];[]
print(c2.write_file_formatter())
# "cal";4;[('exam', 50)];[]

# simulate text file
courses_from_file = c1.write_file_formatter() + '\n' + c2.write_file_formatter() 
# "Bio";4;[('exam', 100)];[]
# "cal";4;[('exam', 50)];[] 

# simulate read from file
for course in courses_from_file.split('\n'):
    c = Course.read_file_formatter(course)
    print(type(c), c)

# <class '__main__.Course'> Bio;4;[('exam', 100)];[]
# <class '__main__.Course'> cal;4;[('exam', 50)];[]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM