Skip to content
Advertisement

Python List Comprehension to Delete Dict in List

JavaScript

I have a json file formatted as above and I am trying to have a function delete an entry based on the username input from a user. I want the file to then be overwritten with the entry removed.

I have tried this:

JavaScript

It partially works as everything in the Credentials section looks normal, but it appends a strange copied piece to the end that breaks the JSON. Say I was removing Grant and I ran this code, my JSON looks like below:

JavaScript

I am relatively new to Python and editing JSONs as well.

Advertisement

Answer

You need to truncate the file after writing:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement