python - add ban reason module error dict object has no attribute append and load and save methods fail to save and load correctly -
can me adding information key in dict , updating information accordingly?
also can give me better method of saving , loading doing currently?
my current method dict key:
def add_banreason(roomname, username, reason): roomname = roomname.lower() username = username.lower() if not roomname in banr.banr_database: banr.banr_database[roomname] = {username:reason} else: banr.banr_database[roomname].append({username:reason})
i error here saying dict object has no attribute append
i lookin result such no error:
{roomname: {username:reason, username:reason, username:reason}, roomname: {username:reason, username:reason, username:reason}}
my current method of saving , loading:
### load database def load(): banr_database = {} open("banr.db","r") t: data = t.read() if data != "": l in data.split("\n"): a,b = l.split("):(",1) c = [w.split(")-(") w in b.split("(:)")] banr_database[a] = c banr.banr_database = banr_database ### save database def save(): l = [] a,b in banr.banr_database.items(): c = "(:)".join([")-(".join(w) w in b]) l.append(a+"):("+c) open("banr.db","w") t: t.write("\n".join(sorted(l)))
what info saved , loaded in format in given example below:
{roomname: {username:reason, username:reason, username:reason}, roomname: {username:reason, username:reason, username:reason}}
however instead getting this:
anime-gir):(k)-(a)-(m)-(i)-(j)-(o)-(u)-(t)-(o)-(u)-(m)-(a)-(3)-(1)-(0
the reason not there , username has been split not in dict format
can please tell me have done wrong , code appropriate here want instead of have?
banr.banr_database[roomname].update({username:reason})
dicts not have append ... have update should want
to save serialize data ... pickle works ... json faster though
import json db_fname = "banr.db" def load(): banr.banr_database = json.load(open(db_fname)) def save(): json.dump(banr.banr_database,open(db_fname,"w"))
Comments
Post a Comment