Home > Back-end >  Create dictionary with data per file
Create dictionary with data per file

Time:03-19

I have a task where I want to plot several plots in matplotlib. Each plot's date is stored in a single file. To handle it better and directly associate things like filepath and leged entry, I want to create a dictionary with the data of each file:

file1 { 'filepath': path/to/file , 'x_data': x[], 'y_data': y[], 'y_legend': 'some_string'}

I have a list of all the input files:

input_files[file1, file2, ..., file_n]

so now I would iterate over the list to create a dictionary for each file. This is the step I am not sure how to do. Any ideas?

for i in input_files:
#create dictionary -- how?

   with open (file1):
       fill_dictionary()

CodePudding user response:

for i in input_files:
my_dict = dict()

   with open (file1):
       my_dict=fill_dictionary()

I would recommend you to store the data in json files. Then you can read in those files as dictionary

import json

with open('path_to_file/data.json', 'r') as f:
  data = json.load(f)

data is a dictionary

  • Related