亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Python json.decoder.JSONDecodeError:額外數據

Python json.decoder.JSONDecodeError:額外數據

慕婉清6462132 2022-12-14 21:13:31
我正在編寫一個充當登錄屏幕的簡單程序。作為其中的一部分,我讓用戶創建一個帳戶,該帳戶將在 JSON 文檔中保存他們的姓名、年齡、城市和登錄信息。我已經能夠很容易地寫入 JSON,但是每當我從文件中讀取時,它都會拋出“額外數據錯誤”并說它在第 2 行第 14 列我正在使用 python 3.8.2,如果有幫助的話這是中的代碼main.pyimport jsondef read(accList):    with open('acc.json') as accountFile:        first = accountFile.read(1)        if not first:            print("No accounts")        else:            accList["account"]=json.load(accountFile)    accountFile.close()    return(accList)def write(accList):    with open('acc.json',"w") as accountFile:        accountFile.write(json.dumps(accList, indent = 4, sort_keys=True))    accountFile.close()def login(accList):    accList=read(accList)    user=input("Please enter your username: ")    password=input("Please enter your password: ")def create(accList):    accList=read(accList)    name=input("Please enter your name: ")    age =input("How old are you: ")    city = input("What city do you live in: ")    user=input("Please create a username: ")    password=input("Please create a  password: ")    accList["account"].append({        "name": name,        "age": age,        "city": city,        "username": user,        "password": password    })    write(accList)def main():    accList = {}    accList["account"] = []    returning=input("Welcome to Cael's login screen.\nDo you already have an account? ")    if(returning.lower()=="yes"):        login(accList)    elif(returning.lower()=="no"):        create(accList)    #else:     #   print("Sorry that's not a valid answer. Please only type \'Yes\' or \'No\'.")main()這是我在 JSON 中的輸出(注意:添加一個帳戶可以正常工作,但之后會拋出錯誤){    "account": [        {            "age": "52",            "city": "Jerome",            "name": "Maynard",            "password": "pass",            "username": "mjkeenan"        }    ]}抱歉,如果這看起來有點愚蠢,或者存在明顯的錯誤。我對整個 JSON 文件還是陌生的
查看完整描述

1 回答

?
尚方寶劍之說

TA貢獻1788條經驗 獲得超4個贊

該錯誤是因為一旦您讀取文件,指針將在末尾,所以這就是發生該錯誤的原因,并且在調試后拋出了另一個錯誤,即dict對象沒有append方法,我只是調試了它


import json


def read(accList):

    with open('acc.json') as accountFile:

        first = accountFile.read(1)

        # This will make the pointer to point at the beggining of the file

        accountFile.seek(0)


        if not first:

            print("No accounts")

        else:

            # this is to access only the account list and not the whole dictionary

            accList["account"]=json.load(accountFile)["account"]

    accountFile.close()

    return(accList)


def write(accList):

    with open('acc.json',"w") as accountFile:

        accountFile.write(json.dumps(accList, indent = 4, sort_keys=True))

    accountFile.close()


def login(accList):

    accList=read(accList)

    user=input("Please enter your username: ")

    password=input("Please enter your password: ")


def create(accList):

    accList=read(accList)

    name=input("Please enter your name: ")

    age =input("How old are you: ")

    city = input("What city do you live in: ")

    user=input("Please create a username: ")

    password=input("Please create a  password: ")

    accList["account"].append({

        "name": name,

        "age": age,

        "city": city,

        "username": user,

        "password": password

    })

    write(accList)


def main():

    accList = {}

    accList["account"] = []

    returning=input("Welcome to Cael's login screen.\nDo you already have an account? ")

    if(returning.lower()=="yes"):

        login(accList)

    elif(returning.lower()=="no"):

        create(accList)

    #else:

     #   print("Sorry that's not a valid answer. Please only type \'Yes\' or \'No\'.")

main()


查看完整回答
反對 回復 2022-12-14
  • 1 回答
  • 0 關注
  • 109 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號