0

I have the following code where file[f] has 3 directory of files. I am trying to create a dictionary made of dictionaries made of arrays. Everything seems to go fine until I try adding the SHEETS dictionary to the FILE dictionary which is where for some reason all the values in the dictionary FILE get replaced.

SHEETS = {}
FILE = {}


f = 0
while f < len(file):
    
    s = 2 #specifies which sheet to start at
    p = 7 #specifies how many how sheets to read
    while s < p:
        
        df = pd.read_excel(file[f], sheet_name = s)
        Z = df.iloc[10:14,4:7].to_numpy()  
             
        SHEETS[s-2] = Z

        s = s + 1
    
    FILE[f]= SHEETS
    f = f + 1
wiggles8x0
  • 17
  • 6
  • Are you saying that you expected `FILE[f] = SHEETS` *not* to replace the previous value of `FILE[f]`? – mkrieger1 May 24 '21 at 19:46
  • Sure, updated original code to hopefully be easier to follow – wiggles8x0 May 24 '21 at 19:47
  • So say when I try to add FILE[1] = SHEETS_x it will replace FILE[0] as well with SHEETS_x which I do not want – wiggles8x0 May 24 '21 at 19:48
  • It's not really apparent from the code you have shown (`FILE` is never defined and it is only assigned to a single time), but presumably you are adding the *same* `SHEETS` object multiple times to `FILE`. – mkrieger1 May 24 '21 at 19:49
  • The SHEETS objects gets updated so it is ever the same when it is being added to FILE,, hence why I am trying to implement a counter on FILE to keep it separate. Both are defined as such SHEETS = {} FILE = {} – wiggles8x0 May 24 '21 at 19:51
  • See also: https://stackoverflow.com/questions/52642538/python-changing-list-in-dictionary-changes-all-lists-no-other-solutions-working, https://stackoverflow.com/questions/13498453/changing-one-dict-value-changes-all-values – mkrieger1 May 24 '21 at 19:53
  • Thanks! Deepcopy seemed to have been the right solution – wiggles8x0 May 24 '21 at 21:10

0 Answers0