Home > Mobile >  Can not update a dictionary
Can not update a dictionary

Time:07-23

I am writing a python program to scan through .csv files of a given directory. I want the number of each word appearance as my output. This is what I have coded by now.

import os
import pandas as pd

root = D:\dir1\dir2\data
ext = '.csv'


dict_napak_file = {} #creating two dictionaries to later try to write data into
dict_napak_dir = {}

for datoteka in os.scandir(root): #going thorugh files in the given directory
    if datoteka.path.endswith(ext): #only do below code if the file ends with .csv
        df = pd.read_csv(datoteka, encoding = 'cp1252')
        fifth_column = df.iloc[:, 4]  # Gets all rows for the fifth column (index starts at 0)
        counts = fifth_column.value_counts()
        dict_napak_file.update(counts)  #this is where the problem starts. It does write in the dictionary. But only for one file
    dict_napak_dir = dict_napak_dir.update(dict_napak_file) #Now I want to merge all dictionaries made while scanning the files into one dictionary to use for further data anayisis

print("done")

I get this error:

--------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [50], in <cell line: 4>()
      8         counts = forth_column.value_counts()
      9         dict_napak_dat.update(counts)
---> 10     dict_napak_dir = dict_napak_dir.update(dict_napak_dat)
     12 print("done")

AttributeError: 'NoneType' object has no attribute 'update'

CodePudding user response:

a.update(b) is a function that does not return anything, i.e. it returns None. It directly adds dictionary b into dictionary a instead. If we store the output in the variable, e.g. c = a.update(b), printing c out will return None. If you print a, however, it should print the merged dictionary in the console. This is the demo code:

a = {'something': 'something2'}
b = {'something3': 'something4'}
k = a.update(b)
print(a)
print(k)

This is the console:

{'something': 'something2', 'something3': 'something4'}
None
  • Related