将字典写入文件后,我目前无法正确显示文件。对于此程序,输入文件需要具有以下格式: ID:日期:Dayskept:产品名称,例如 1:12/12/2011:12:A
这是我第一次将示例文件读入字典时很好,但是一旦我将字典保存到一个新文件中并尝试打开这个文件,我就会得到输出:1:"date":12/12/2011,"life ":12, "名称":A
在将字典写入文件之前,是否有一种简单的方法来格式化字典中的数据?感谢您提供的任何建议
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()