import os import json # Save data def save_data(dataset, filetype, filename): if dataset: directory = "./ejde_buffer/" + filetype + "/" os.makedirs(directory, exist_ok=True) filepath = os.path.join(directory, filename) with open(filepath, "w", encoding='utf-8') as json_file: json.dump(dataset, json_file, indent=4) print(filetype + " data have been added to", filepath) # Write into output files def transform_data(): def read(folder_path, output_files): # Create new folders os.makedirs('./ejde_buffer/Article_output/', exist_ok=True) os.makedirs('./ejde_buffer/Author_output/', exist_ok=True) data_oldest = [] data_2010_2014 = [] data_2015_2020 = [] data_newest = [] data_no_date = [] data_integrate = [] for filename in os.listdir(folder_path): if filename.endswith('.json'): file_path = os.path.join(folder_path, filename) with open(file_path, 'r', encoding='utf-8') as file: data = json.load(file) for Dict in data: if Dict.get('volume') is not None or Dict.get('affiliation', [{}])[0].get('year', 0) != '': # Select data if int(Dict.get('volume') or Dict.get('affiliation', [{}])[0].get('year', 0)) <= 2009: data_oldest.append(Dict) elif int(Dict.get('volume') or Dict.get('affiliation', [{}])[0].get('year', 0)) <= 2014: data_2010_2014.append(Dict) elif int(Dict.get('volume') or Dict.get('affiliation', [{}])[0].get('year', 0)) <= 2020: data_2015_2020.append(Dict) else: data_newest.append(Dict) else: data_no_date.append(Dict) data_integrate.append(data_oldest) data_integrate.append(data_2010_2014) data_integrate.append(data_2015_2020) data_integrate.append(data_newest) data_integrate.append(data_no_date) # Transfer Data = [data_oldest, data_2010_2014, data_2015_2020, data_newest, data_no_date, data_integrate] for index in range(0, 6): with open(output_files[index], 'w', encoding='utf-8') as file: json.dump(Data[index], file, indent=4) # The path of reading author_folder_path = './ejde_buffer/Author_TS' article_folder_path = './ejde_buffer/Article_TS' # The path of storage author_output_file = [ './ejde_buffer/Author_output/EJDE_Author_output_file(oldest).json', './ejde_buffer/Author_output/EJDE_Author_output_file(2010-2014).json', './ejde_buffer/Author_output/EJDE_Author_output_file(2015-2020).json', './ejde_buffer/Author_output/EJDE_Author_output_file(newest).json', './ejde_buffer/Author_output/EJDE_Author_output_file(no date).json', './ejde_buffer/Author_output/EJDE_Author_output_file(integration).json' ] article_output_file = [ './ejde_buffer/Article_output/EJDE_Article_output_file(oldest).json', './ejde_buffer/Article_output/EJDE_Article_output_file(2010-2014).json', './ejde_buffer/Article_output/EJDE_Article_output_file(2015-2020).json', './ejde_buffer/Article_output/EJDE_Article_output_file(newest).json', './ejde_buffer/Article_output/EJDE_Article_output_file(no date).json', './ejde_buffer/Article_output/EJDE_Article_output_file(integration).json' ] # Read and write into files read(author_folder_path, author_output_file) read(article_folder_path, article_output_file) # End print("\nData has been written into files.") # Delete files in temporary storage area def delete_data(): folder_paths = ['./ejde_buffer/Author_TS', './ejde_buffer/Article_TS'] for folder_path in folder_paths: file_names = os.listdir(folder_path) for file_name in file_names: file_path = os.path.join(folder_path, file_name) if os.path.isfile(file_path): os.remove(file_path) os.rmdir(folder_path) print('\nAttention: The temporary storage files have been deleted!')