本人是初入Python界的小白,可以说是白中白。因工作需要,批量对数据处理,Get到Python这项小技能格外重要。因避免涉及公司和个人隐私,故举个与本人无关的工作示例和代码,供大家参考,希望大家能交流互相学习。这是我第一篇CSDN博客。学习Python的大佬们都知道有2句非常有名的话语,1中1英。以后我的博客都会出现这2句话,以‘英’开始,以‘中’结束。
Talk is cheap,please show your code.
However,为了让大家更简单易懂代码结构和运行,还是上几张图解释一下。
1.目标目录下的表格文件(.xlsx)
2.随便打开一个表格看一下:(所有表格的格式是一致的)
3.目的:把目标文件夹下的所有.xlsx文件中Customer中为‘B’客户的都提取出来,做客户一致性筛查处理,时间起始是2020-3-20到2020-4-5,我们看到表格中是没有文件客户是谁负责的信息,所以我们要把提取的文件的名称加到各自的筛选数据中。OK,let us Go!
Now,Beginning!
Talk is cheap,show me your code.好吧我刚说过了。
import pandas as pd
import os
import re
import openpyxl
from openpyxl import load_workbook
import time
#人机交互,信息收集,判断输入数据的准确与否,选择日期范围,需要筛选的客户,可自行设置
start_date = input('请输入日期起始点:(格式1978/4/1)\n')
end_date = input('请输入日期终止点:(格式同上)\n')
if start_date > end_date:
print('有误,请检查!')
time.sleep(1)
print('程序结束,请重新Running.')
else:
Customer = input('请输入您要汇总的客户名称:(格式ABCD)\n')
time.sleep(1)
if Customer != 'A'and \
Customer != 'B'and \
Customer != 'C' and \
Customer != 'D'and \
Customer != 'E' and \
Customer != 'F' and \
Customer != 'G' and \
Customer != 'H':
print('有误,请检查!')
time.sleep(1)
print('程序结束,请重新Running.')
else:
print('信息获取完毕,数据正在提取\n')
time.sleep(1)
print('...\n......\n.........')
#开始导入Timer,Running
start = time.time()
#读取路径到上层文件夹,因为要遍历该文件夹下所有的表格
inpath = "C:/Users/Administrator/Desktop/Python/read/5"
#输出目标路径
outpath = "C:/Users/Administrator/Desktop/Python/write/5/客户一致性筛查.xlsx"
#后缀定义
suffix = '.xlsx'
#核心代码
file_list = os.listdir(inpath)
print('在目标路径' + inpath + '\n找到后缀为' + suffix + '的文件:\n'+ str(file_list))
#定义数据框
df_raw= pd.DataFrame()
for file in file_list:
if file.endswith(suffix):
path = os.path.join(inpath,file)
#正则提取文件名称
order_name = re.findall(r"(.+?)\.",file)
#读取表格数据
df0 = pd.read_excel(path)
#日期筛选及客户筛选
df0['Date'] = df0['Date'].apply(pd.to_datetime,format='%Y-%m-%d')#具体时间:%H:%S:%f
df00 = df0.loc[(df0['Date'] >= start_date) & (df0['Date'] <= end_date) & (df0['Customer'] == Customer)]
length = len(df00)
order_name_col = order_name * length
#加销售人员的名字
df00.insert(1,"From",order_name_col)
#拼接数据
df_raw = pd.concat([df_raw,df00],axis=0,sort=False)
print(df_raw)
df_raw.to_excel(outpath)
#停止计时
end = time.time()
print('本次任务用时'+str(round(end-start,3))+'s')
print('输出文件位置:\n'+outpath)
4.可以来看看运行过程:
C:\Users\Administrator\PycharmProjects\untitled\venv\Scripts\python.exe C:/Users/Administrator/PycharmProjects/untitled/CustomerTotal.py
请输入日期起始点:(格式1978/4/1)
2020/3/20
请输入日期终止点:(格式同上)
2020/4/5
请输入您要汇总的客户名称:(格式ABCD)
B
信息获取完毕,数据正在提取
...
......
.........
在目标路径C:/Users/Administrator/Desktop/Python/read/5
找到后缀为.xlsx的文件:
['Arry.xlsx', 'Bob.xlsx', 'Cathy.xlsx', 'Diavd.xlsx', 'Jessia.xlsx', 'John.xlsx', 'Lily.xlsx', 'Michcal.xlsx', 'Oliver.xlsx', 'Rebbita.xlsx', 'Sabria.xlsx']
Date From Customer Address Requirment Contact
10 2020-03-22 Arry B 上海 2层阳光板 1864567343
13 2020-03-25 Arry B 上海 4层阳关板 1864567346
18 2020-03-30 Arry B 上海 阳光板 1874567342
24 2020-04-05 Arry B 上海 阳光板 1834567341
Date From Customer Address Requirment Contact
10 2020-03-22 Arry B 上海 2层阳光板 1864567343
13 2020-03-25 Arry B 上海 4层阳关板 1864567346
18 2020-03-30 Arry B 上海 阳光板 1874567342
24 2020-04-05 Arry B 上海 阳光板 1834567341
10 2020-03-22 Bob B 上海 2层阳光板 1864567343
13 2020-03-25 Bob B 上海 4层阳关板 1864567346
18 2020-03-30 Bob B 上海 阳光板 1874567342
24 2020-04-05 Bob B 上海 阳光板 1834567341
Date From Customer Address Requirment Contact
10 2020-03-22 Arry B 上海 2层阳光板 1864567343
13 2020-03-25 Arry B 上海 4层阳关板 1864567346
18 2020-03-30 Arry B 上海 阳光板 1874567342
24 2020-04-05 Arry B 上海 阳光板 1834567341
10 2020-03-22 Bob B 上海 2层阳光板 1864567343
13 2020-03-25 Bob B 上海 4层阳关板 1864567346
18 2020-03-30 Bob B 上海 阳光板 1874567342
24 2020-04-05 Bob B 上海 阳光板 1834567341
10 2020-03-22 Cathy B 上海 2层阳光板 1864567343
13 2020-03-25 Cathy B 上海 4层阳关板 1864567346
18 2020-03-30 Cathy B 上海 阳光板 1874567342
24 2020-04-05 Cathy B 上海 阳光板 1834567341
Date From Customer Address Requirment Contact
10 2020-03-22 Arry B 上海 2层阳光板 1864567343
13 2020-03-25 Arry B 上海 4层阳关板 1864567346
18 2020-03-30 Arry B 上海 阳光板 1874567342
24 2020-04-05 Arry B 上海 阳光板 1834567341
10 2020-03-22 Bob B 上海 2层阳光板 1864567343
13 2020-03-25 Bob B 上海 4层阳关板 1864567346
18 2020-03-30 Bob B 上海 阳光板 1874567342
24 2020-04-05 Bob B 上海 阳光板 1834567341
10 2020-03-22 Cathy B 上海 2层阳光板 1864567343
13 2020-03-25 Cathy B 上海 4层阳关板 1864567346
18 2020-03-30 Cathy B 上海 阳光板 1874567342
24 2020-04-05 Cathy B 上海 阳光板 1834567341
10 2020-03-22 Diavd B 上海 2层阳光板 1864567343
13 2020-03-25 Diavd B 上海 4层阳关板 1864567346
18 2020-03-30 Diavd B 上海 阳光板 1874567342
24 2020-04-05 Diavd B 上海 阳光板 1834567341
Date From Customer Address Requirment Contact
10 2020-03-22 Arry B 上海 2层阳光板 1864567343
13 2020-03-25 Arry B 上海 4层阳关板 1864567346
18 2020-03-30 Arry B 上海 阳光板 1874567342
24 2020-04-05 Arry B 上海 阳光板 1834567341
10 2020-03-22 Bob B 上海 2层阳光板 1864567343
13 2020-03-25 Bob B 上海 4层阳关板 1864567346
18 2020-03-30 Bob B 上海 阳光板 1874567342
24 2020-04-05 Bob B 上海 阳光板 1834567341
10 2020-03-22 Cathy B 上海 2层阳光板 1864567343
13 2020-03-25 Cathy B 上海 4层阳关板 1864567346
18 2020-03-30 Cathy B 上海 阳光板 1874567342
24 2020-04-05 Cathy B 上海 阳光板 1834567341
10 2020-03-22 Diavd B 上海 2层阳光板 1864567343
13 2020-03-25 Diavd B 上海 4层阳关板 1864567346
18 2020-03-30 Diavd B 上海 阳光板 1874567342
24 2020-04-05 Diavd B 上海 阳光板 1834567341
10 2020-03-22 Jessia B 上海 2层阳光板 1864567343
13 2020-03-25 Jessia B 上海 4层阳关板 1864567346
18 2020-03-30 Jessia B 上海 阳光板 1874567342
24 2020-04-05 Jessia B 上海 阳光板 1834567341
Date From Customer Address Requirment Contact
10 2020-03-22 Arry B 上海 2层阳光板 1864567343
13 2020-03-25 Arry B 上海 4层阳关板 1864567346
18 2020-03-30 Arry B 上海 阳光板 1874567342
24 2020-04-05 Arry B 上海 阳光板 1834567341
10 2020-03-22 Bob B 上海 2层阳光板 1864567343
13 2020-03-25 Bob B 上海 4层阳关板 1864567346
18 2020-03-30 Bob B 上海 阳光板 1874567342
24 2020-04-05 Bob B 上海 阳光板 1834567341
10 2020-03-22 Cathy B 上海 2层阳光板 1864567343
13 2020-03-25 Cathy B 上海 4层阳关板 1864567346
18 2020-03-30 Cathy B 上海 阳光板 1874567342
24 2020-04-05 Cathy B 上海 阳光板 1834567341
10 2020-03-22 Diavd B 上海 2层阳光板 1864567343
13 2020-03-25 Diavd B 上海 4层阳关板 1864567346
18 2020-03-30 Diavd B 上海 阳光板 1874567342
24 2020-04-05 Diavd B 上海 阳光板 1834567341
10 2020-03-22 Jessia B 上海 2层阳光板 1864567343
13 2020-03-25 Jessia B 上海 4层阳关板 1864567346
18 2020-03-30 Jessia B 上海 阳光板 1874567342
24 2020-04-05 Jessia B 上海 阳光板 1834567341
10 2020-03-22 John B 上海 2层阳光板 1864567343
13 2020-03-25 John B 上海 4层阳关板 1864567346
18 2020-03-30 John B 上海 阳光板 1874567342
24 2020-04-05 John B 上海 阳光板 1834567341
Date From Customer Address Requirment Contact
10 2020-03-22 Arry B 上海 2层阳光板 1864567343
13 2020-03-25 Arry B 上海 4层阳关板 1864567346
18 2020-03-30 Arry B 上海 阳光板 1874567342
24 2020-04-05 Arry B 上海 阳光板 1834567341
10 2020-03-22 Bob B 上海 2层阳光板 1864567343
13 2020-03-25 Bob B 上海 4层阳关板 1864567346
18 2020-03-30 Bob B 上海 阳光板 1874567342
24 2020-04-05 Bob B 上海 阳光板 1834567341
10 2020-03-22 Cathy B 上海 2层阳光板 1864567343
13 2020-03-25 Cathy B 上海 4层阳关板 1864567346
18 2020-03-30 Cathy B 上海 阳光板 1874567342
24 2020-04-05 Cathy B 上海 阳光板 1834567341
10 2020-03-22 Diavd B 上海 2层阳光板 1864567343
13 2020-03-25 Diavd B 上海 4层阳关板 1864567346
18 2020-03-30 Diavd B 上海 阳光板 1874567342
24 2020-04-05 Diavd B 上海 阳光板 1834567341
10 2020-03-22 Jessia B 上海 2层阳光板 1864567343
13 2020-03-25 Jessia B 上海 4层阳关板 1864567346
18 2020-03-30 Jessia B 上海 阳光板 1874567342
24 2020-04-05 Jessia B 上海 阳光板 1834567341
10 2020-03-22 John B 上海 2层阳光板 1864567343
13 2020-03-25 John B 上海 4层阳关板 1864567346
18 2020-03-30 John B 上海 阳光板 1874567342
24 2020-04-05 John B 上海 阳光板 1834567341
10 2020-03-22 Lily B 上海 2层阳光板 1864567343
13 2020-03-25 Lily B 上海 4层阳关板 1864567346
18 2020-03-30 Lily B 上海 阳光板 1874567342
24 2020-04-05 Lily B 上海 阳光板 1834567341
Date From Customer Address Requirment Contact
10 2020-03-22 Arry B 上海 2层阳光板 1864567343
13 2020-03-25 Arry B 上海 4层阳关板 1864567346
18 2020-03-30 Arry B 上海 阳光板 1874567342
24 2020-04-05 Arry B 上海 阳光板 1834567341
10 2020-03-22 Bob B 上海 2层阳光板 1864567343
13 2020-03-25 Bob B 上海 4层阳关板 1864567346
18 2020-03-30 Bob B 上海 阳光板 1874567342
24 2020-04-05 Bob B 上海 阳光板 1834567341
10 2020-03-22 Cathy B 上海 2层阳光板 1864567343
13 2020-03-25 Cathy B 上海 4层阳关板 1864567346
18 2020-03-30 Cathy B 上海 阳光板 1874567342
24 2020-04-05 Cathy B 上海 阳光板 1834567341
10 2020-03-22 Diavd B 上海 2层阳光板 1864567343
13 2020-03-25 Diavd B 上海 4层阳关板 1864567346
18 2020-03-30 Diavd B 上海 阳光板 1874567342
24 2020-04-05 Diavd B 上海 阳光板 1834567341
10 2020-03-22 Jessia B 上海 2层阳光板 1864567343
13 2020-03-25 Jessia B 上海 4层阳关板 1864567346
18 2020-03-30 Jessia B 上海 阳光板 1874567342
24 2020-04-05 Jessia B 上海 阳光板 1834567341
10 2020-03-22 John B 上海 2层阳光板 1864567343
13 2020-03-25 John B 上海 4层阳关板 1864567346
18 2020-03-30 John B 上海 阳光板 1874567342
24 2020-04-05 John B 上海 阳光板 1834567341
10 2020-03-22 Lily B 上海 2层阳光板 1864567343
13 2020-03-25 Lily B 上海 4层阳关板 1864567346
18 2020-03-30 Lily B 上海 阳光板 1874567342
24 2020-04-05 Lily B 上海 阳光板 1834567341
10 2020-03-22 Michcal B 上海 2层阳光板 1864567343
13 2020-03-25 Michcal B 上海 4层阳关板 1864567346
18 2020-03-30 Michcal B 上海 阳光板 1874567342
24 2020-04-05 Michcal B 上海 阳光板 1834567341
Date From Customer Address Requirment Contact
10 2020-03-22 Arry B 上海 2层阳光板 1864567343
13 2020-03-25 Arry B 上海 4层阳关板 1864567346
18 2020-03-30 Arry B 上海 阳光板 1874567342
24 2020-04-05 Arry B 上海 阳光板 1834567341
10 2020-03-22 Bob B 上海 2层阳光板 1864567343
13 2020-03-25 Bob B 上海 4层阳关板 1864567346
18 2020-03-30 Bob B 上海 阳光板 1874567342
24 2020-04-05 Bob B 上海 阳光板 1834567341
10 2020-03-22 Cathy B 上海 2层阳光板 1864567343
13 2020-03-25 Cathy B 上海 4层阳关板 1864567346
18 2020-03-30 Cathy B 上海 阳光板 1874567342
24 2020-04-05 Cathy B 上海 阳光板 1834567341
10 2020-03-22 Diavd B 上海 2层阳光板 1864567343
13 2020-03-25 Diavd B 上海 4层阳关板 1864567346
18 2020-03-30 Diavd B 上海 阳光板 1874567342
24 2020-04-05 Diavd B 上海 阳光板 1834567341
10 2020-03-22 Jessia B 上海 2层阳光板 1864567343
13 2020-03-25 Jessia B 上海 4层阳关板 1864567346
18 2020-03-30 Jessia B 上海 阳光板 1874567342
24 2020-04-05 Jessia B 上海 阳光板 1834567341
10 2020-03-22 John B 上海 2层阳光板 1864567343
13 2020-03-25 John B 上海 4层阳关板 1864567346
18 2020-03-30 John B 上海 阳光板 1874567342
24 2020-04-05 John B 上海 阳光板 1834567341
10 2020-03-22 Lily B 上海 2层阳光板 1864567343
13 2020-03-25 Lily B 上海 4层阳关板 1864567346
18 2020-03-30 Lily B 上海 阳光板 1874567342
24 2020-04-05 Lily B 上海 阳光板 1834567341
10 2020-03-22 Michcal B 上海 2层阳光板 1864567343
13 2020-03-25 Michcal B 上海 4层阳关板 1864567346
18 2020-03-30 Michcal B 上海 阳光板 1874567342
24 2020-04-05 Michcal B 上海 阳光板 1834567341
10 2020-03-22 Oliver B 上海 2层阳光板 1864567343
13 2020-03-25 Oliver B 上海 4层阳关板 1864567346
18 2020-03-30 Oliver B 上海 阳光板 1874567342
24 2020-04-05 Oliver B 上海 阳光板 1834567341
Date From Customer Address Requirment Contact
10 2020-03-22 Arry B 上海 2层阳光板 1864567343
13 2020-03-25 Arry B 上海 4层阳关板 1864567346
18 2020-03-30 Arry B 上海 阳光板 1874567342
24 2020-04-05 Arry B 上海 阳光板 1834567341
10 2020-03-22 Bob B 上海 2层阳光板 1864567343
13 2020-03-25 Bob B 上海 4层阳关板 1864567346
18 2020-03-30 Bob B 上海 阳光板 1874567342
24 2020-04-05 Bob B 上海 阳光板 1834567341
10 2020-03-22 Cathy B 上海 2层阳光板 1864567343
13 2020-03-25 Cathy B 上海 4层阳关板 1864567346
18 2020-03-30 Cathy B 上海 阳光板 1874567342
24 2020-04-05 Cathy B 上海 阳光板 1834567341
10 2020-03-22 Diavd B 上海 2层阳光板 1864567343
13 2020-03-25 Diavd B 上海 4层阳关板 1864567346
18 2020-03-30 Diavd B 上海 阳光板 1874567342
24 2020-04-05 Diavd B 上海 阳光板 1834567341
10 2020-03-22 Jessia B 上海 2层阳光板 1864567343
13 2020-03-25 Jessia B 上海 4层阳关板 1864567346
18 2020-03-30 Jessia B 上海 阳光板 1874567342
24 2020-04-05 Jessia B 上海 阳光板 1834567341
10 2020-03-22 John B 上海 2层阳光板 1864567343
13 2020-03-25 John B 上海 4层阳关板 1864567346
18 2020-03-30 John B 上海 阳光板 1874567342
24 2020-04-05 John B 上海 阳光板 1834567341
10 2020-03-22 Lily B 上海 2层阳光板 1864567343
13 2020-03-25 Lily B 上海 4层阳关板 1864567346
18 2020-03-30 Lily B 上海 阳光板 1874567342
24 2020-04-05 Lily B 上海 阳光板 1834567341
10 2020-03-22 Michcal B 上海 2层阳光板 1864567343
13 2020-03-25 Michcal B 上海 4层阳关板 1864567346
18 2020-03-30 Michcal B 上海 阳光板 1874567342
24 2020-04-05 Michcal B 上海 阳光板 1834567341
10 2020-03-22 Oliver B 上海 2层阳光板 1864567343
13 2020-03-25 Oliver B 上海 4层阳关板 1864567346
18 2020-03-30 Oliver B 上海 阳光板 1874567342
24 2020-04-05 Oliver B 上海 阳光板 1834567341
10 2020-03-22 Rebbita B 上海 2层阳光板 1864567343
13 2020-03-25 Rebbita B 上海 4层阳关板 1864567346
18 2020-03-30 Rebbita B 上海 阳光板 1874567342
24 2020-04-05 Rebbita B 上海 阳光板 1834567341
Date From Customer Address Requirment Contact
10 2020-03-22 Arry B 上海 2层阳光板 1864567343
13 2020-03-25 Arry B 上海 4层阳关板 1864567346
18 2020-03-30 Arry B 上海 阳光板 1874567342
24 2020-04-05 Arry B 上海 阳光板 1834567341
10 2020-03-22 Bob B 上海 2层阳光板 1864567343
13 2020-03-25 Bob B 上海 4层阳关板 1864567346
18 2020-03-30 Bob B 上海 阳光板 1874567342
24 2020-04-05 Bob B 上海 阳光板 1834567341
10 2020-03-22 Cathy B 上海 2层阳光板 1864567343
13 2020-03-25 Cathy B 上海 4层阳关板 1864567346
18 2020-03-30 Cathy B 上海 阳光板 1874567342
24 2020-04-05 Cathy B 上海 阳光板 1834567341
10 2020-03-22 Diavd B 上海 2层阳光板 1864567343
13 2020-03-25 Diavd B 上海 4层阳关板 1864567346
18 2020-03-30 Diavd B 上海 阳光板 1874567342
24 2020-04-05 Diavd B 上海 阳光板 1834567341
10 2020-03-22 Jessia B 上海 2层阳光板 1864567343
13 2020-03-25 Jessia B 上海 4层阳关板 1864567346
18 2020-03-30 Jessia B 上海 阳光板 1874567342
24 2020-04-05 Jessia B 上海 阳光板 1834567341
10 2020-03-22 John B 上海 2层阳光板 1864567343
13 2020-03-25 John B 上海 4层阳关板 1864567346
18 2020-03-30 John B 上海 阳光板 1874567342
24 2020-04-05 John B 上海 阳光板 1834567341
10 2020-03-22 Lily B 上海 2层阳光板 1864567343
13 2020-03-25 Lily B 上海 4层阳关板 1864567346
18 2020-03-30 Lily B 上海 阳光板 1874567342
24 2020-04-05 Lily B 上海 阳光板 1834567341
10 2020-03-22 Michcal B 上海 2层阳光板 1864567343
13 2020-03-25 Michcal B 上海 4层阳关板 1864567346
18 2020-03-30 Michcal B 上海 阳光板 1874567342
24 2020-04-05 Michcal B 上海 阳光板 1834567341
10 2020-03-22 Oliver B 上海 2层阳光板 1864567343
13 2020-03-25 Oliver B 上海 4层阳关板 1864567346
18 2020-03-30 Oliver B 上海 阳光板 1874567342
24 2020-04-05 Oliver B 上海 阳光板 1834567341
10 2020-03-22 Rebbita B 上海 2层阳光板 1864567343
13 2020-03-25 Rebbita B 上海 4层阳关板 1864567346
18 2020-03-30 Rebbita B 上海 阳光板 1874567342
24 2020-04-05 Rebbita B 上海 阳光板 1834567341
10 2020-03-22 Sabria B 上海 2层阳光板 1864567343
13 2020-03-25 Sabria B 上海 4层阳关板 1864567346
18 2020-03-30 Sabria B 上海 阳光板 1874567342
24 2020-04-05 Sabria B 上海 阳光板 1834567341
本次任务用时0.867s
输出文件位置:
C:/Users/Administrator/Desktop/Python/write/5/客户一致性筛查.xlsx
Process finished with exit code 0
成功,Well Done! 只要0.867s!没有什么是比时间还珍贵。
But,还有一句中文没有说哦!好吧,太熟悉了,
人生苦短,我用Python!