Answer In the end I ended up going to pickle in the end
Well, therefore, with some tips on another matter, I asked that I was told to use pickle to save the dictionary to a file.
The dictionary that I tried to save to a file was
members = {'Starspy' : 'SHSN4N', 'Test' : 'Test1'}
When pickle saved it to a file ... it was a format
(dp0 S'Test' p1 S'Test1' p2 sS'Test2' p3 S'Test2' p4 sS'Starspy' p5 S'SHSN4N' p6 s.
Can you give me an alternative way to save a string in a file?
This is the format that I would like to save to
members = {'Starspy': 'SHSN4N', 'Test': 'Test1'}
The code:
import sys import shutil import os import pickle tmp = os.path.isfile("members-tmp.pkl") if tmp == True: os.remove("members-tmp.pkl") shutil.copyfile("members.pkl", "members-tmp.pkl") pkl_file = open('members-tmp.pkl', 'rb') members = pickle.load(pkl_file) pkl_file.close() def show_menu(): os.system("clear") print "\n","*" * 12, "MENU", "*" * 12 print "1. List members" print "2. Add member" print "3. Delete member" print "99. Save" print "0. Abort" print "*" * 28, "\n" return input("Please make a selection: ") def show_members(members): os.system("clear") print "\nNames", " ", "Code" for keys in members.keys(): print keys, " - ", members[keys] def add_member(members): os.system("clear") name = raw_input("Please enter name: ") code = raw_input("Please enter code: ") members[name] = code output = open('members-tmp.pkl', 'wb') pickle.dump(members, output) output.close() return members
python dictionary save pickle
wKavey Feb 04 2018-11-11T00: 00Z
source share