我正在重写我的一些代码,觉得必须有一个更好的更有活力的方式来做下面的事情。目前你可以看到我正在创建一个直接基于行数并从那里添加值的条件。但是,我不想为多个值if row_count == 3:
if row_count == 4:
制作静态条件等。我肯定必须有更有效的方法来实现这一目标。任何指针都会受到赞赏。
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)
答案 0 :(得分:0)
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)