Python basic syntax--json storage data

import json 
# with open("C:\\Users\\yahui.zhao\\Desktop\\aaa666.json","w") as jsonfile: 
# users = '[{"uname":"zhangsan","upwd ":"123"},{"uname":"lisi","upwd":"123"},{"uname":"wangwu","upwd":"123"}]'#Don’t forget the outermost Single quotes 
#jsonfile.write(users) 
#Run the above program once to generate the file, otherwise the file will be overwritten if you run it again. 
#The json file must be generated by a program, otherwise an error will be reported when running next 

def readjson(dizhi): 
    with open (dizhi,"r") as jsondata1: 
       jsondata=jsondata1.read() 
    pythondata=json.loads(jsondata) 
    return pythondata 
def writejson(pythondata3,dizhi1): 
    jsondata1=json.dumps(pythondata3)#This step is required before the json file can be written 
    . print("Finally written value type", type(jsondata1)) 
    with open(dizhi1,"w") as jsondata:
        jsondata.write(jsondata1) 
    userpwd1=input("Please enter password")
    # zuizhong=jsondata.write(jsondata1)# The write() function cannot be assigned a value in the end. If you want to assign a value, read it out first 
    # print(zuizhong) 
def denglu(): 
    nameisin=0 
    username=input("Please enter the user name ") 
    userpwd=input("Please enter your password") 
    pythonuser=readjson("C:\\Users\\yahui.zhao\\Desktop\\aaa666.json") 
    for user1 in pythonuser: 
        if username==user1["uname "]: 
            if userpwd==user1["upwd"]: 
                nameisin=1 
                print("Login successful") 
    if nameisin==0: 
        print("Login failed") 

def adduser(): 
    username1=input("Please enter the user name") 
    dictuser1={"uname":username1,"upwd":userpwd1}
    pythondatas=readjson("C:\\Users\\yahui.zhao\\Desktop\\aaa666.json") 
    print("String before printing",pythondatas) 
    print(type(pythondatas)) 
    pythondatas.append(dictuser1)#append function cannot be assigned a value and is added to the list There is no return after 
    print("Word to be entered",pythondatas) 
    writejson(pythondatas, "C:\\Users\\yahui.zhao\\Desktop\\aaa666.json") 
    pythondatas3=readjson("C:\\Users\ \yahui.zhao\\Desktop\\aaa666.json") 
    print("last",pythondatas3) 

if __name__=='__main__': 
    denglu() 
    adduser()


Guess you like

Origin blog.csdn.net/qq_40333984/article/details/125501876