'Add a value to an empty key in a created dictionary [closed]

so I basically need to add a value From a txt file to the keys in output_dic and be in the following format for both years

the idea is to get data from a txt that's organised in rows and columns and procese it to get the data organised in the following output


                                        2020                 2021        
Max Volume (Date)                       'value added'       'value2 added'
Min Volume (Date)

import matplotlib.pyplot as plt
import time     
import pandas as pd


def main(): 
    
    again = "y"     
    
    while again.lower() == 'y' or again.lower() == "yes":
    
    
    
        output_dic = {'Max Volume (Date)': ["", "", ""],
                  'Min Value (Date)' : ["" , "", ""],
                  'Lowest Open (Date)' : ["", "", ""] ,
                  'Highest Close (Date)' : ["", "", ""],
                  'Highest Monthly Average (Months)' : ["", "" , ""],
                  'Lowest Monthly Average (Months)' : ["", "", ""],
                  'Annual Average (Months)' : ["", "", ""]}
    
        print("\n{:^91}\n".format("Apple Stock Analysis 2020 and 2021"))
        print("{:^31} {:^20} {:^20}" .format(" ", "2020", "2021" , "total"))
        print("-" * 91)
        
        
        for k, v in output_dic.items():
                y2020, y2021, y2022 = v 
                print( "{:<31} {:^20} {:^20} {:^20}" .format(k , y2020, y2021, y2022))
        break
    
    
    output_dic.update({'Max Volume (Date)' :  })
    
    print(output_dic)


Solution 1:[1]

You can add values to a dictionary by simply assigning the values to a keys, if for example a key already exists, then the new value would replace the old one, for example:

output_dic['Max Volume (Date)'] = [0, 1, 2]

Just to be complete I would write a sample code snippet on how to replace values read from a .txt file with values already assigned in a dictionary:

output_dic = {'Max Volume (Date)': ["", "", ""],
                'Min Value (Date)' : ["" , "", ""],
                'Lowest Open (Date)' : ["", "", ""] ,
                'Highest Close (Date)' : ["", "", ""],
                'Highest Monthly Average (Months)' : ["", "" , ""],
                'Lowest Monthly Average (Months)' : ["", "", ""],
                'Annual Average (Months)' : ["", "", ""]}

keys_list = ['Max Volume (Date)', 'Min Value (Date)', 'Lowest Open (Date)', 'Highest Close (Date)', 'Highest Monthly Average (Months)', 'Lowest Monthly Average (Months)', 'Annual Average (Months)']

input_file_address = 'E://file_name.txt'

with open(input_file_address, 'r') as input_file:
    index = 0
    for line in input_file:
        line_stripped = line.strip() # .txt files' lines usually end with \n and they could also have whitespace at the beginning by mistake, this line removes these issues
        
        if line_stripped == '': # if the line is empty, move to the next line
            continue
        
        line_splitted = line_stripped.split(' ') # If the values are separated by whitespace, this line would convert a string of for example 'a b c' to a list of ['a', 'b', 'c'] 
        
        key = keys_list[index]

        output_dic[key] = line_splitted

        index += 1

print(output_dic)

Assuming the .txt file is formatted as below:

value_1, value_2, value_3 ......................... .........................

where each line corresponds to a different parameter for example "Min Value" and so on.

And finally I would like to make a recommandations: I think .txt is not a suitable file format for these types of operations, I personally prefer .csv, it's easier to parse since the formatting is unversal, unlike a .txt file where anyone can write anything in any format.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ash