I am saving a dictionary of student names as keys and grades lists as values. I am attempting to write the values to a file. At the moment I am writing them as strings.
def save_records(students, filename): #saves student records to a file out_file = open(filename, "w") for x in students.keys(): out_file.write(x + " " + str(students[x]) + "\n") out_file.close() After saving the file, I try to read it back. The pertinent part of the read out is below.
while True: in_line = in_file.readline() if not in_line: break #deletes line read in in_line = in_line[:-1] #initialize grades list in_line = in_line.split() name = in_line[0] students[name] = map(int, in_line[1:]) The read out code works well for normal text files that are pre-formatted. The format of the textfile is: key (whitespace) values separated by whitespace "\n". I would like to know how to write in to a text file by combining string and list elements.