i have been having some problems understanding where i went wrong in my codes. I have been trying to print out the output of my codes into a json file but apparently, my list is empty because it has this "default" thing printed out. How do i change it such that the books map has Book Id and Serial no. part into the empty {} instead? I already did my own research but unfortunately none of the forums that i went to provided an answer.
Here's my output:
{"books": {}, "_default":
{
"1": {Book ID: XXXX bookid 1 XXXX},
"2": {Serial No.: XXX serialno 1 XXXX}
},
{
"1": {Book ID: XXX bookid 2 XXXX},
"2": {Serial No.: XXX serial no. 2 XXX}
}
}
Here are my codes:
with open("/home/pi/Desktop/json/pillar1.json", 'w+'):
db = TinyDB('/home/pi/Desktop/json/pillar1.json')
table = db.table('Books')
db.insert_multiple([{'Book ID' : variable}, {'Serial no' : variable]}])
There is always a _default
table, described here
Operations like db.insert_multiple
will happen on the _default
table. Which you actually show in your file sniplet correctly.
Table "books"
is empty
"books": {}
and the inserts
have take place on the _default
table
"_default":
{
"1": {Book ID: XXXX bookid 1 XXXX},
"2": {Serial No.: XXX serialno 1 XXXX}
},
{
"1": {Book ID: XXX bookid 2 XXXX},
"2": {Serial No.: XXX serial no. 2 XXX}
}
In order to insert in a table, you can do the following:
with open("/home/pi/Desktop/json/pillar1.json", 'w+'):
db = TinyDB('/home/pi/Desktop/json/pillar1.json')
table = db.table('books')
table.insert_multiple([{'Book ID' : variable}, {'Serial no' : variable]}])
note the table.insert_multiple
in stead of db.insert_multiple
[EDIT]
you can set your own default table as described here
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.