Appending data to a json file

Dave dboland9 at fastmail.fm
Tue Apr 4 13:23:52 EDT 2017


On 04/04/2017 10:17 AM, Michael Torrie wrote:
> On 04/03/2017 11:31 PM, dieter wrote:
>> Dave <dboland9 at fastmail.fm> writes:
>>
>>> I created a python program that gets data from a user, stores the data
>>> as a dictionary in a list of dictionaries.  When the program quits, it
>>> saves the data file.  My desire is to append the new data to the
>>> existing data file as is done with purely text files.
>>
>> Usually, you cannot do that:
>> "JSON" stands for "JavaScript Object Notation":
>
> That's assuming he's using JSON; he never specified what he's using to
> represent data as plain text.  Though I suspect you're correct, for all
> we know he could just be writing data using his own text representation
> or writing to an ini file.  And in his case it sounds like JSON is not
> an ideal method for saving his data since he's wanting to only append
> data, not read it in his program.
>
> In the future, Dave, please provide all the information pertaining to
> the problem so we can give accurate advice. Don't make us guess or
> assume we can all infer this information.
>
I did.  Please see the subject - it is json, not some bastardized 
version of it.  While I didn't explicitly say that the data is read back 
in, it is - when the program starts.  The user can enter more data, then 
save the data to the list (append).  Then, like any other program, they 
can save the data to file.

I don't care for the idea of replacing the data file for every save. My 
preference would to append to the existing data file - makes more sense. 
However, that is not how json works.  So, I'm considering other 
alternatives for a data file structure.  Paired data (key and value) is 
not really required, but my feeling is that it makes the data file more 
robust.  A database seems a little over the top for this situation since 
there is no querying of data - just load and save.

Dave,


More information about the Python-list mailing list