I am rewriting some code of mine and feel there must be a better more dynamic way to do the below. Currently as you can see I am creating a condition based directly on the row count and adding values from there. However I don't want to have to make static conditions for multiple values if row_count == 3:
if row_count == 4:
etc. I'm positive there must be a more efficient way to achieve this. Any pointers would be appreciated.
for root, dirs, files in os.walk(main):
filters = '*specificname*.csv'
for filename in fnmatch.filter(files, filters):
df = pd.read_csv(os.path.join(root, filename),error_bad_lines=False)
row_count = len(df.index)
device_dic = collections.defaultdict()
if row_count == 2:
device_dic[df.iloc[0][1]] = {}
device_dic[df.iloc[0][1]]['item1'] = df.iloc[0][2]
device_dic[df.iloc[0][1]]['item2'] = df.iloc[0][3]
device_dic[df.iloc[1][1]] = {}
device_dic[df.iloc[1][1]]['item1'] = df.iloc[1][2]
device_dic[df.iloc[1][1]]['item2'] = df.iloc[1][3]
for key in device_dic.iterkeys():
device.append(key)
def func1(device_dict):
device_dic[df.iloc[0][1]] = {}
device_dic[df.iloc[0][1]]['item1'] = df.iloc[0][2]
device_dic[df.iloc[0][1]]['item2'] = df.iloc[0][3]
device_dic[df.iloc[1][1]] = {}
device_dic[df.iloc[1][1]]['item1'] = df.iloc[1][2]
device_dic[df.iloc[1][1]]['item2'] = df.iloc[1][3]
for key in device_dic.iterkeys():
device.append(key)
# Or whatever you want to return
return device
def func2(device_dict):
# your code here
pass
# Store each function in a dict
process_map = {2 : func1, 3: func2, 4: func2, ...}
for root, dirs, files in os.walk(main):
filters = '*specificname*.csv'
for filename in fnmatch.filter(files, filters):
df = pd.read_csv(os.path.join(root, filename),error_bad_lines=False)
row_count = len(df.index)
device_dic = collections.defaultdict()
# Could also use get() to provide a default processing func
process_func = process_map[row_count]
result = process_func(device_dict)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.