在列表中追加[Python]

在列表中追加[Python],python,list,append,attributeerror,Python,List,Append,Attributeerror,有人能给我解释一下为什么我会有这样的错误信息: 第27行,在 课程列表。附加(项目添加) AttributeError:'str'对象没有属性'append 提前感谢您的帮助 import os import json dossier_courant = os.path.dirname(__file__) dossier = os.path.join (dossier_courant, "liste.json") if os.path.exists(dossier):

有人能给我解释一下为什么我会有这样的错误信息:

第27行,在 课程列表。附加(项目添加)

AttributeError:'str'对象没有属性'append

提前感谢您的帮助

import os
import json

dossier_courant = os.path.dirname(__file__)

dossier = os.path.join (dossier_courant, "liste.json")

if os.path.exists(dossier):
    with open (dossier, "r") as f:
        liste_de_courses = json.load(f)
else:
    liste_de_courses = []

affichage = """
\t1: Add a item
\t2: Delete a item
\t3: Show a item
\t4: Empty a item
\t5: Over
"""
choix_utilisateur = "0"
while choix_utilisateur != "5":

    choix_utilisateur = input (affichage)
    if choix_utilisateur == "1":
        item_add = input ("What would you like to add ? ")
        liste_de_courses.append(item_add)
    elif choix_utilisateur == "2":
        item_delete = input ("What item do you wish to remove ? ")
        if item_delete in liste_de_courses:
            liste_de_courses.remove(item_delete)
print ("Bye")
在此之后添加
打印(键入(列出课程))

打开(档案,“r”)作为f:
liste_de_courses=json.load(f)

您的JSON文件存储的是JSON字符串(Python
str
),而不是JSON数组(Python
list
)。您需要修复输入文件。

课程列表不是列表,而是字符串。
我对json一无所知,但请检查json.load(f)是否返回什么以及返回哪种类型。

json.load()方法(在“load”中不带“s”)用于从文件读取json编码的数据并将其转换为Python字典。您试图将其附加为列表。@JoeFerndz:它不必是Python
dict
;合法的JSON包括简单的双引号字符串或(方括号)数组。谢谢:)我很理解@ShadowRangerit's a type>我发现了问题,这是我的JSON文件,谢谢你的帮助:)如果它帮助你将此答案标记为已接受,请。