Reputation: 747
I am trying to take in all the files at a given path, and order them based on my data title names. So my data title names are:
data_titles = ['CPU','Physical_Disk','Memory','Network']
The files at this given path are named like 'CPU_data.txt'
and 'Memory_data.txt'
but there are also some that have more than one file per data title for example 'Physical_Disk_data_1.txt'
and 'Physical_Disk_data_2.txt'.
I am trying to create a dicitonary in the style of:
{'Network': 'Network_data.txt',
'Physical_Disk': ['Physical_Disk_data_1.txt','Physical_Disk_data_2.txt'],
'CPU': 'CPU_data.txt',
'Memory': 'Memory_data.txt'}
i.e not overwriting older values
However I keep getting the error AttributeError: 'dict' object has no attribute 'update'
, if I use append instead of update I get a similar error AttributeError: 'dict' object has no attribute 'append'
table_csv_files={}
for file_names in os.listdir(Data_folder):
for name in data_titles:
if name in file_names:
if name in table_csv_files:
table_csv_files[name].update(file_names)
# Have also tried table_csv_files.append({name:file_names})
else:
table_csv_files[name]=file_names
print table_csv_files
What am I doing wrong?
Upvotes: 1
Views: 5274
Reputation: 3454
According to Python documentation it's better to use defaultdict(), see the answer by Moses Koledoye
You cannot append to a dictionary. You can append to a value corresponding to a key if that value is a list (or antoher object that implement the append method).
I think this is the best way to make the code easy to follow. The function setdefault add a new list only if the key is not present.
if name in file_names:
table_csv_files.setdefault(name, [])
table_csv_files[name].append(file_names)
Upvotes: 0
Reputation: 78536
Use a defaultdict
whose default value is an empty list
, then you can append
to the dictionary values without worrying about which keys already exist in your dictionary.
from collections import defaultdict
table_csv_files = defaultdict(list)
for file_names in os.listdir(Data_folder):
for name in data_titles:
if name in file_names:
table_csv_files[name].append(file_names)
print table_csv_files
# {'CPU': ['CPU_data.txt'], 'Memory': ['Memory_data_1.txt', 'Memory_data_2.txt'], 'Physical_Disk': ['Physical_Disk_data.txt'], 'Network': ['Network_data.txt']}
Upvotes: 2
Reputation: 7102
here is a simple example of how you can add/update another value to an existing key in dictionary.
my_dict = {'name': 'Rajiv Sharma', 'city': 'Delhi'}
print my_dict
my_dict['city'] = 'Kuala Lumpur'
print my_dict
output:
{'city': 'Delhi', 'name': 'Rajiv Sharma'}
{'city': 'Kuala Lumpur', 'name': 'Rajiv Sharma'}
Upvotes: -1
Reputation: 3575
The problem is that your values are sometimes strings (e.g. for Network
) and sometimes lists (e.g. for Physical_Disk
). If you make sure that they are always lists you can easily append to them:
data_titles = ['CPU','Physical_Disk','Memory','Network']
table_csv_files={}
listdir_output = ['Network_data.txt', 'Physical_Disk_data_1.txt','Physical_Disk_data_2.txt', 'CPU_data.txt', 'Memory_data.txt']
for file_names in listdir_output:
for name in data_titles:
if name in file_names:
if name in table_csv_files:
table_csv_files[name].append(file_names)
else:
table_csv_files[name] = [file_names]
print table_csv_files
Output:
{'Memory': ['Memory_data.txt'], 'Physical_Disk': ['Physical_Disk_data_1.txt', 'Physical_Disk_data_2.txt'], 'Network': ['Network_data.txt'], 'CPU': ['CPU_data.txt']}
Upvotes: 2