I am currently having problems displaying a file correctly once i have written a dictionary to the file. For this program the input file needs to have the format:
ID: Date: Dayskept: ProductName e.g. 1:12/12/2011:12:A

This is fine the first time I read the example file into a dictionary, but once i save the dictionay into a new file and try to open this file i get the output:
1:"date":12/12/2011, "life":12, "name":A

Is there an easy way to format the data in the dictionary before it is written to file?
Thanks for any advice given :)

def loadProduct(fileName):
    global cheeseDictionary
    f = open(fileName,"r")
    line = f.readline()         # Reads line from file
    while line:
        line = line[:-1]
        data = split(line,":")  # Splits line when there is a colon
        cheeseDictionary[data[0]] = {"date":data[1], "life":data[2], "name":data[3]} # Stores each split item
        line = f.readline()     # Next line
    f.close()
    print cheeseDictionary

def saveProduct(fileName):
    global cheeseDictionary
    f = open(fileName,"w")
    pickle.dump(cheeseDictionary, f)
    f.close()

Recommended Answers

All 6 Replies

You could use ordered data type like namedtuple instead of unordered dictionary.

Later on in the code I will need to use the life value for a calculation, would that still be possible if i used a tuple?

Thanks I'll try that then.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.