第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

Python json.decoder.JSONDecodeError:額外數(shù)據(jù)

Python json.decoder.JSONDecodeError:額外數(shù)據(jù)

慕婉清6462132 2022-12-14 21:13:31
我正在編寫一個(gè)充當(dāng)?shù)卿浧聊坏暮?jiǎn)單程序。作為其中的一部分,我讓用戶創(chuàng)建一個(gè)帳戶,該帳戶將在 JSON 文檔中保存他們的姓名、年齡、城市和登錄信息。我已經(jīng)能夠很容易地寫入 JSON,但是每當(dāng)我從文件中讀取時(shí),它都會(huì)拋出“額外數(shù)據(jù)錯(cuò)誤”并說它在第 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 中的輸出(注意:添加一個(gè)帳戶可以正常工作,但之后會(huì)拋出錯(cuò)誤){    "account": [        {            "age": "52",            "city": "Jerome",            "name": "Maynard",            "password": "pass",            "username": "mjkeenan"        }    ]}抱歉,如果這看起來有點(diǎn)愚蠢,或者存在明顯的錯(cuò)誤。我對(duì)整個(gè) JSON 文件還是陌生的
查看完整描述

1 回答

?
尚方寶劍之說

TA貢獻(xiàn)1788條經(jīng)驗(yàn) 獲得超4個(gè)贊

該錯(cuò)誤是因?yàn)橐坏┠x取文件,指針將在末尾,所以這就是發(fā)生該錯(cuò)誤的原因,并且在調(diào)試后拋出了另一個(gè)錯(cuò)誤,即dict對(duì)象沒有append方法,我只是調(diào)試了它


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()


查看完整回答
反對(duì) 回復(fù) 2022-12-14
  • 1 回答
  • 0 關(guān)注
  • 108 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)