Python 2.7.8 - 字典到文件并返回样式

Python 2.7.8 - Dictionary to file and back with style

 def __init__(self):
    self.bok = {'peter': 123, 'maria': 321, 'harry': 888}

 def save(self):
    file = open('test.txt', 'w')
    file.write(str(self.bok))
    file.close()
    print "File was successfully saved."

 def load(self):
    try:
        file = open('test.txt', 'r')
        newdict = eval(file.read())
        self.bok = newdict
        file.close()
        print "File was successfully loaded."
    except IOError:                                 
        print "No file was found."
        return

如何在文本文件中使其看起来像这样:
value1:key1
value2:key2
==>
123:peter
321:maria
目前看起来很正常,字典:
{'peter': 123, 'maria': 321, 'harry': 888}
但是会出现的问题是加载文件,因为它看起来不再像字典了?

(加载 = 将字典从 .txt 文件加载到 self.bok{})
(保存 = 将词典保存到 .txt 文件)

import csv

def save(self, filepath):
    with open(filepath, 'w') as fout:
        outfile = csv.writer(fout, delimiter=":")
        for k,v in self.bok.iteritems():
            outfile.writerow([v,k])

def load(self, filepath):
    if not os.path.isfile(filepath):
        print "No file was found"
        return
    with open(filepath) as infile:
        for v,k in csv.reader(infile, delimiter=":"):
            self.bok[k] = v

没有csv的任何帮助:

def save(self, filepath):
    with open(filepath, 'w') as outfile:
        for k,v in self.bok.iteritems():
            outfile.write("{}:{}\n".format(v,k))

def load(self, filepath):
    if not os.path.isfile(filepath):
        print "No file was found"
        return
    with open(filepath) as infile:
        for line in infile:
            v,k = line.strip().split(":")
            self.bok[k] = v