Using a nested for loop to iterate over the data variables

Each of the data variables should be extracted from the old entry and placed in the new entry. This can be done using a nested for loop, an additional for loop inside the base for loop. You can use a nested for loop to iterate over each of the data variables in the variables array.

Within the base for loop, create a new nested for loop that iterates over the elements in the variables array. In the nested for loop, write a statement that sets the value of each variable in the new array to the value of the variable in the old array:

...
for old_entry in issues:
new_entry = {}
for variable in variables:
new_entry[variable] = old_entry[variable]

It may be helpful to print out content at this point just to make sure that the loop is set up properly. This is optional, but if you would like to test the code, you can add a print function after the nested for loop to verify that each of the variables are added to the new data entry. This is demonstrated in the following continuation of process_data.py:

...
for old_entry in issues:
new_entry = {}
for variable in variables:
new_entry[variable] = old_entry[variable]
print(new_entry)

At this stage, with the print function from the previous example included, the following is the output on my system:

5. Finally, place each new entry into the new_scf_data array using the Python list append() method as follows:

...
for old_entry in issues:
new_entry = {}
for variable in variables:
new_entry[variable] = old_entry[variable]
# print(new_entry)
new_scf_data.append(new_entry)
The append function is called by writing .append() directly after the name of a Python list as follows:
<array_variable>.append(<value>)
Calling the append function places the value that is passed in at the end of the array.

So far process_data.py creates a Python list containing the new data entries with the extracted data variables, however the new data disappears after the program is finished running. In order to preserve the data for future use, you will need to output a file containing the revised dataset. In the next section, I will walk through the steps for outputting data from python to a JSON file.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset