objects = [] #the object list
with open("path/to/file") as reader: #opens the file
for line in reader: #iterates the lines
objects.append(Object(*line.strip().split(", "))) #appends the objects to the list
关于最后一行的更多细节,可以这样打开:
parts = line.strip.split(", ") #each item in the line
obj = Object(parts[0], parts[1]) #like doing Objects(item1, item2) from the line.
objects.append(obj) #add the object to the list
这个答案创建了一个对象列表,对象是通过遍历文件中的每一行并将每一行拆分为两个项目,然后使用它们来构造对象。你知道吗
关于最后一行的更多细节,可以这样打开:
相关问题 更多 >
编程相关推荐