1

I use a loop to every time add a new list called lst to my txt file with this

with open('database.txt','a+') as myfile:
    json.dump(lst,myfile)

but I want JSON to every time put a new list in a new row. How to do that? So I could later extract line by line and return it to list in program.

Or, can I insert ',' somehow between lists, so when I load the file and read it, get usable list of lists.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343

1 Answers1

2

Write a newline after every JSON dump:

with open('database.txt','a+') as myfile:
    json.dump(lst, myfile)
    myfile.write('\n')

Then, provided you read the file line-by-line later, you can load the JSON into Python objects again.

Community
  • 1
  • 1
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343