1

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.

1
  • I'm not sure what you mean by "I would like to know how to write in to a text file by combining string and list elements." Can you explain? It seems like your algorithm is working but you want to make it "better". Am I correct? Commented Feb 8, 2014 at 20:17

4 Answers 4

1

If you have control over writing the data, I would recommend using a well-established format, such as JSON or INI. This would allow you to make use of common libraries, such as the json or ConfigParser modules, respectively.

Sign up to request clarification or add additional context in comments.

Comments

1

Would it not be easier to use something like pythons pickle which is for storing things like dicts

...and then pretty print output to a separate file?

It's hard to say without knowing how you plan on using this...

Comments

1

Since students[name] = map(int, in_line[1:]), i assume you want to print the items of the list student[x] with whitespaces inbetween.

You could use the str.join method

' '.join(map(str,students[x])) 

Comments

0

You may want to consider using Comma Separated Value files (aka csv files) instead of plain text files, as these provide a more structured way to read and write your data. Once written, you can open them in a spreadsheet program like Excel to view and edit their contents.

Re-writing your functions to work with csv files, and assuming you are using Python 2.x, we get something like:

import csv def save_records(students, filename): # note that csv files are binary so on Windows you # must write in 'wb' mode; also note the use of `with` # which ensures the file is closed once the block is # exited. with open(filename, 'wb') as f: # create a csv.writer object csv_out = csv.writer(f) for name, grades in students.iteritems(): # write a single data row to the file csv_out.writerow([name]+grades) def read_records(filename): students = dict() # note that we must use 'rb' to read in binary mode with open(filename, 'rb') as f: # create a csv.reader object csv_in = csv.reader(f) for line in csv_in: # name will have type `str` name = line[0] grades = [int(x) for x in line[1:]] # update the `students` dictionary students[name] = grades return students 

3 Comments

Wouldn't it be easier to use the csv module in the python standard library to read and write .csv files?
@SethMMorton: that's what I've used, but you may have read my post mid-edit, before I put in the import statement?
Yup, that must have been what happened, because when I saw it, it wasn't there.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.