python - Writing to file problems and displaying file contents -


After writing a dictionary in the file, I am currently having trouble displaying a file properly. Input for this program File should contain format: id: date: day: product name eg 1: 12/12/2011: 12: A

This is the first time when I read an example file in a dictionary, but once I save the dictionay in a new file and open this file Try to get the output: 1: "Date": 12/12/2011, "Life": 12, "Name": A

Does it have been written before the data formatted in the dictionary An easy way to file? Thanks for any advice

  def loadProduct (fileName): global cheeseDictionary f = open (fileName, "r") line = f.readline () # reads the line from the file while the line : Line = data [= 1] data = divided (line, ":") # colon line when there is a colon paneidedition [data [0]] = {"date": data [1], "life": data [2 ], "Name": data [3]} # Store each divided item lines = f.readline () # Next line f.close () Print Pennijiedef Def rescue product (file name): Global Cheeses Thief F = Open (filename, "W") Pickle.dump (cheeseDictionary, f) f.close ()    

If you have a specific format you want, you need to write the code to emit that format. (I do not know what you're trying to do with the code , which produces a binary format that is not what you're saying, there is no similarity to it.)

For example, you can redefine the saveProduct :

  def saveProduct (fileName, cheeseDictionary): f = open (fileName, "W") I sorted (cheeseDictionary.keys): v = cheeseDictionary [i] f.write ("% s:% s:% s:% s \ n"% (i, v ["date" ], V [life], v ["name"]))    

Comments