创作灵感:公司有几千台云主机,分配给公司几百个业务人员使用,但是呢有些人申请了他不用,导致资源严重浪费。我们从后台拉一下数据,有4百多台云主机未使用,我们要从我们登记的总表把人员部门名字和IP对应上。一个一个的搜的话,估计要几个小时,而且累手啊。因此有了一下创作灵感。
import pandas
import re
# 读取txt文件数据,保存到ip_txt_list列表
ip_txt_list = []
ip_list = []
password_list = []
path1 = r'C:\Users\Administrator\Desktop\虚拟机ip地址.txt'
with open(path1, 'rb') as r1:
red_list = r1.readlines()
# 处理读取的txt文件数据,保存到ip_txt_list列表
for i in red_list:
rstr = i.decode()
r1 = rstr.strip()
ip_txt_list.append(r1)
# 提取整理ip_txt_list列表中数据,保存到ip_list列表
for i in ip_txt_list:
if i != '':
ip1 = re.match('^10[.]18[.][0-9]{,3}[.][0-9]{,3}', i).group()
ip_list.append(ip1)
# 读取表格
filename = r'C:\Users\Administrator\Desktop\虚拟机ip-1.xlsx'
df = pandas.read_excel(filename, sheet_name=0, usecols=['ip', '部门', '使用人'])
# 确认原始数据中不包含的IP数据,单独列出
screening_ip = []
not_include_ip = []
load_ip = list(df.get('ip'))
for i in ip_list:
if i in load_ip:
print(i)
ip_index = ip_list.index(i)
screening_ip.append(i)
else:
not_include_ip.append(i)
# 数据筛选
screening_df = df[df['ip'].isin(screening_ip)]
# 创建DataFrame对象
new_row = pandas.DataFrame({'ip地址': not_include_ip})
# 保存筛选数据,生成新的数据表格
with pandas.ExcelWriter(r'C:\Users\Administrator\Desktop\虚拟机ip-2.xlsx', engine='openpyxl', mode='w') as w:
screening_df.to_excel(w, index=False, sheet_name='未登录企微ip')
new_row.to_excel(w, index=False, sheet_name='未登记ip')
print('保存成功')