I'm trying to take a dictionary popped from a list and append it to another list and save it as a new file.
If the file is blank I can create a new list of dictionaries and save the file, but if there is an existing list of dictionaries dont know how to check the file for a list (it will either be blank or a list of dicts), and if there is, append the file. I can't just append the file because its a list of dictionaries and the trailing ]
would break the list.
My question is how do you check the file if it already contains a list and if it does, how do you append it? (ie do you have to loop through the exiting list in the file, therefore creating a new list, and append that list with the pop and saving it?... or is there a better way?)
import json
import os
def main():
with open('file1.json', 'r') as fin:
user_list = json.load(fin)
temp = user_list.pop(0)
open_if_not_exists("processed.json")
with open('processed.json', 'w') as processed_file:
my_list = []
try:
data = json.load(processed_file)
for i in data:
my_list.append(i)
my_list.append(temp)
except (IOError):
my_list.append(temp)
json.dump(my_list, processed_file)
with open('file1.json', 'w') as fout:
json.dump(user_list, fout)
def open_if_not_exists(filename):
try:
fd = os.open(filename, os.O_CREAT | os.O_EXCL | os.O_WRONLY)
except OSError as e:
if e.errno == 17:
return None
else:
raise
else:
return os.fdopen(fd, 'w')
if __name__ == "__main__":
main()
Sample data
[{"id": 1234, "screen_name": "UserName"}, {"id": 5678, "screen_name": "Bobo2022"}, {"id": 9101, "screen_name": "ReallyDude"}, {"id": 1213, "screen_name": "SMH"}, {"id": 1415, "screen_name": "IAskQuestions"}]
CodePudding user response:
As a quick modification to your code...
- It still naively assumes that if a file opens, it contains a valid json array
import json
import os
def main():
processed_list = []
new_list = []
new_dict = {}
with open('file1.json', 'r') as fin:
new_list = json.load(fin)
new_dict = new_list.pop(0)
try:
with open('processed.json', 'r') as processed_file:
processed_list = json.load(processed_file)
except FileNotFoundError:
processed_list = [ new_dict ]
else:
processed_list.append(new_dict)
with open('processed.json', 'w') as fout:
json.dump(processed_list, fout)
with open('file1.json', 'w') as fout:
json.dump(new_list, fout)
if __name__ == "__main__":
main()