如何将新列表追加到JSON文件中的现有字典

时间:2020-09-27 16:56:39

标签: python json list dictionary

我是Python的新手,我试图列出其中有学生的教室列表。但是我正在尝试使用变量将其附加到JSON文件中的字典中。我当前的JSON文件如下所示:

[{"History": ["sally", "billy", "tommy"]}]

但是我想添加一个新列表,使其看起来像这样:

[{"Hisotry": ["sally", "billy", "tommy"], "Calculus": ["billy", "sammy"]}]

我有这段代码,其中变量classRoom是班级,studentName是学生。我尝试通过更改dict的数据类型将新列表追加到整个文件,但是我一直收到错误消息,说“关键字不能是表达式”。当我尝试运行它时。

with open('students.json') as p:
      data = json.load(p)
if type (data) is dict:
      data = [data]
#classRoom = [studentName]
data.append([str(classRoom)] = [str(studentName)])
with open ('students.json', 'w') as outfile:
          json.dump(data,outfile)
 print "Success added new student to classroom"

我还尝试创建列表,然后再将其添加到字典中,进行 classRoom = [studentName],然后进行附加data.append(classRoom),但仅添加列表,而不添加classRoom名称。因此,JSON文件最终看起来像这样。

[{"History": ["sally", "billy", "tommy"], ["sally"]}]

我正在尝试在文件中同时包含教室名称和学生名称,但是我不确定尝试将新列表添加到文件中做错了什么。我该如何做或我缺少什么有帮助吗?

2 个答案:

答案 0 :(得分:1)

` java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.aplayer/com.example.aplayer.MainActivity}: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3319)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415)
    at android.app.ActivityThread.access$1100(ActivityThread.java:229)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:7406)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
 Caused by: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
    at android.database.CursorWindow.nativeGetString(Native Method)
    at android.database.CursorWindow.getString(CursorWindow.java:451)
    at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
    at android.database.CursorWrapper.getString(CursorWrapper.java:137)
    at com.example.aplayer.MainActivity.GetAllSounds(MainActivity.java:111)
    at com.example.aplayer.MainActivity.Check_Permission(MainActivity.java:82)
    at com.example.aplayer.MainActivity.onCreate(MainActivity.java:69)
    at android.app.Activity.performCreate(Activity.java:6904)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3266)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415) 
    at android.app.ActivityThread.access$1100(ActivityThread.java:229) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:148) 
    at android.app.ActivityThread.main(ActivityThread.java:7406) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)`

这是错误的,您不能分配给函数调用。我也不确定data.append([str(classRoom)] = [str(studentName)]) 的所有转换。

在列表的第一个字典中简单地分配为新键(假设str是字符串classRoom,而"Calculus"是列表studentName):

["billy", "sammy"]

答案 1 :(得分:0)

您数据结构是词典列表。而且您想添加到词典中而不是列表中。

在python list中接受方法append。如果将任何内容添加到列表中,它将保留在列表中。

要添加的键值是字典中的元素,即列表的元素,因此,如果要更新字典,则需要先访问字典,然后将新值键添加到字典中。

向字典添加新元素如下所示

>>> d = {}
>>> d["item1"] = "value1"
>>> print(d)
{"item1":"value1"}

您的代码可以如下所示

data[0][classRoom] = [list of students]