提取Excel表格中指定列数据
定向筛选所需数据
多表数据合并功能
多表数据统计排行
生成成绩分析图表
def get_files_name():
file_list = os.listdir('C:/data')
return file_list
def get_specified_data(dictory, operation, col_name, value):
new_dict = {}
if col_name != "省市":
for key in dictory.keys():
value = float(value)
flag = 0
if operation == '>':
if dictory[key][col_name] > value:
flag = 1
elif operation == '>=':
if dictory[key][col_name] >= value:
flag = 1
elif operation == '=':
if dictory[key][col_name] == value:
flag = 1
elif operation == '<=':
if dictory[key][col_name] <= value:
flag = 1
elif operation == '<':
if dictory[key][col_name] < value:
flag = 1
else:
flag = 0
if flag == 1:
new_dict[key] = {}
new_dict[key] = dictory[key]
else:
for key in dictory.keys():
flag = 0
if operation == '>':
if key > value:
flag = 1
elif operation == '>=':
if key >= value:
flag = 1
elif operation == '=':
if key == value:
flag = 1
elif operation == '<=':
if key <= value:
flag = 1
elif operation == '<':
if key < value:
flag = 1
else:
flag = 0
if flag == 1:
new_dict[key] = {}
new_dict[key] = dictory[key]
return new_dict
def show_data(dictory):
try:
keys = list(dictory[list(dictory.keys())[0]].keys())
except:
print("当前数据为空")
return
head = ['区域']
head.extend(keys)
table = PrettyTable(head)
for key in dictory.keys():
line = [key]
for key_2 in keys:
line.append(dictory[key][key_2])
table.add_row(line)
print(table)
def save(dictory):
name = input("文件名:")
book = xlwt.Workbook()
sheet = book.add_sheet('Sheet1', cell_overwrite_ok=True)
keys = list(data[list(data.keys())[0]].keys())
head = ["区域"]
head.extend(keys)
for h in range(len(head)):
sheet.write(0, h, head[h])
cities = list(dictory.keys())
for city in range(len(cities)):
sheet.write(city + 1, 0, cities[city])
for key in range(len(keys)):
sheet.write(city + 1, key + 1, dictory[cities[city]][keys[key]])
book.save('C:/data/' + name + '.xls')
print("保存成功"
def sort_data(dictory, key, reverse):
data = dictory
if not reverse:
data = dict(sorted(data.items(), key=lambda d: d[1][key], reverse=False)) # 字典的升序
else:
data = dict(sorted(data.items(), key=lambda d: d[1][key], reverse=True)) # 字典的降序
return data
def draw_plot(dictory):
font = {'family': 'MicroSoft Yahei', 'weight': 'normal', 'size': 10}
matplotlib.rc('font', **font)
index = numpy.arange(len(dictory.keys()))
color = [(150/ 256, 75/ 256, 150/ 256,0.5),(100/ 256, 150/ 256, 100/ 256,0.5),
(0/360, 120/360,130/360,0.5),(0,0,0,0.5)]
first_key = list(dictory.keys())
first_key = first_key[0]
cols = list(dictory[first_key].keys())
data = []
for i in range(len(cols)):
data.append([])
for key in dictory.keys():
for col in range(len(cols)):
data[col].append(dictory[key][cols[col]])
offset = -1 / 4
for i in range(len(cols)):
plt.bar(index + offset, data[i], color=color[i], width=1 / 4)
offset += 1 / 4
plt.xticks(index, dictory.keys())
plt.legend(cols)
plt.show()