Python Advanced #15 - Developing a dictionary from values in external text file

This post demonstrates how to develop a dictionary from values in an external text file.  For this example, the values are set to the flag of "true" or "false".  It may be desirable to store these values in an external text file to instruct the program whether to perform a computation or not without having users altering these flags within the code.

The external text file is named, "dicts from text.txt".  The contents of this text file are shown below.

The following is performed in the code shown below:

  • Each line is read and stored in a temporary list.
  • The values are split based on the colon between them.
  • Any extra spaces are stripped and the values are added in key:value pairs in the dictionary named "flag_dict".
  • The temporary list is cleared after each line is read into the dictionary.
  • For this example, I used print statements based on the values; however, computations could be performed based on the values.

The output from this program is shown below.


Table Rock Dam

Table Rock Dam