python初學習之json檔案訪問

2021-09-25 05:20:36 字數 861 閱讀 7893

畢業了,前段時間準備畢業事宜。

剛來到研究生學校,在學習python。

import json

def hello_user():

username = input("請輸入您的姓名:")

if judge_user(username):

print(username+"您在系統名單中!")

else:

print(username + "您未在系統名單中!")

write_user(username)

def judge_user(username):

'''用於判斷json使用者表是否建立以及輸入使用者是否在此表中'''

try:

with open('aa.json','r') as file:

lines = json.load(file)

except:

print("第一次使用該系統,使用者表未建立!馬上建立!")

return none

else:

if username in lines:

return true

else:

return none

def write_user(username):

'''寫入使用者名字'''

try:

with open('aa.json', 'w') as file:

json.dump(username,file)

except:

print('您的名字未能成功寫入!')

else:

print('您的名字成功寫入!')

hello_user()

Python高階學習筆記之JSON

使用json庫 json 是一種輕量級的文字資料交換格式。與 xml 相比,擁有更小 更快 更易解析的特點。其結構和 python 的list dict有點相似。資料表示 json 中資料都以名稱 值的形式表示,名稱包括在一對雙引號 中,值則有多種形式,多條資料之間用逗號,隔開。這種表示方式與 py...

Python學習 重點模組之json

注意 json不能轉換類,不能轉換函式 json.dumps 實現檔案寫入,字串轉換 寫入檔案當然是json字串樓 實際上,json.dumps 只是幫我們做了乙個字串的轉換,把字典轉換為了json格式的字串而已 dict 字典 dict1 json,json.loads dict name 錯誤 ...

初學python之迴圈

while 當滿足條件則進入迴圈體 while condition block 例 1 2 3 4 flag 10 whileflag print flag flag 1 flag為真值,則為條件滿足,當flag直接為0的時候則為假,則不滿足while判斷 如果是負數也為真,因為只有0為假 列印結果...