老污龟们,我想用pyhthon统计excel表格内的重读数据并赋值到新的表格里,应该怎么做
#coding=gbkimport openpyxl
from openpyxl.utils import column_index_from_string
filename = r'./waf策略统计.xlsx'
tongji = openpyxl.load_workbook( filename )
#——————————————服务器ip:10.254.221.140————————————————————————————————————————
tongji1 = tongji['Sheet1']
maxtongji_hang = tongji1.max_row
daochu = openpyxl.load_workbook( r'./waf导出日志.xlsx' )
daochu1 = daochu['Sheet1']
maxdaochu_hang = daochu1.max_row
maxdaochu_lie = daochu1.max_column
for i in range(2,maxdaochu_hang + 1):
tongji1.cell(maxtongji_hang + 1,column_index_from_string('A')).value = maxtongji_hang + 0
if daochu1.cell(i,column_index_from_string('C')).value == '10.254.221.140':
tongji1.cell(2,column_index_from_string('B')).value = daochu1.cell(i,column_index_from_string('C')).value
maxtongji_hang += 1
tongji.save('./xin.xlsx')
比如我想统计导出日志的表格内10.254.221.140这个ip,总共出现多少次应该怎么写,本人小白,求大佬鞭策 本帖最后由 mb0221 于 2020-11-12 16:36 编辑
{:5_108:} {:5_100:} 在线急,等!{:5_105:} 本帖最后由 逃兵 于 2020-11-12 17:01 编辑
中间加了几行代码,是要统计这个吗
#coding=gbk
import openpyxl
from openpyxl.utils import column_index_from_string
filename = r'./waf策略统计.xlsx'
tongji = openpyxl.load_workbook( filename )
#——————————————服务器ip:10.254.221.140————————————————————————————————————————
tongji1 = tongji['Sheet1']
maxtongji_hang = tongji1.max_row
daochu = openpyxl.load_workbook( r'./waf导出日志.xlsx' )
daochu1 = daochu['Sheet1']
maxdaochu_hang = daochu1.max_row
maxdaochu_lie = daochu1.max_column
for i in range(2,maxdaochu_hang + 1):
tongji1.cell(maxtongji_hang + 1,column_index_from_string('A')).value = maxtongji_hang + 0
if daochu1.cell(i,column_index_from_string('C')).value == '10.254.221.140':
tongji1.cell(2,column_index_from_string('B')).value = daochu1.cell(i,column_index_from_string('C')).value
maxtongji_hang += 1
times = 0
file_value={}
for each_line in daochu1:
for each in each_line:
if each.value != None:
file_value = str(each.value)
for i in file_value:
if '10.254.221.140' in file_value:
times+=1
print('10.254.221.140 出现了%s次'%times)
print(i)
tongji.save('./xin.xlsx')
逃兵 发表于 2020-11-12 16:52
中间加了几行代码,是要统计这个吗
哇,太厉害了大佬,我百度了一天了没结果,差不多,我就想统计C列的话,然后把统计出来的次数,放到tongji表格的第二行K列,应该怎么写 有点敏感,这样发个列子可以脑补一下,多谢大佬们理解{:5_96:}
站点ID 防护对象ID 服务器IP
1559642578 2594951412 10.254.221.140
xxxx xxxx 下面有重复的还有不同的ip地址 本帖最后由 逃兵 于 2020-11-12 17:46 编辑
下班了,明天再说
#coding=gbk
import openpyxl
from openpyxl.utils import column_index_from_string
filename = r'./waf策略统计.xlsx'
tongji = openpyxl.load_workbook( filename )
#——————————————服务器ip:10.254.221.140————————————————————————————————————————
tongji1 = tongji['Sheet1']
maxtongji_hang = tongji1.max_row
daochu = openpyxl.load_workbook( r'./waf导出日志.xlsx' )
daochu1 = daochu['Sheet1']
maxdaochu_hang = daochu1.max_row
maxdaochu_lie = daochu1.max_column
for i in range(2,maxdaochu_hang + 1):
tongji1.cell(maxtongji_hang + 1,column_index_from_string('A')).value = maxtongji_hang + 0
if daochu1.cell(i,column_index_from_string('C')).value == '10.254.221.140':
tongji1.cell(2,column_index_from_string('B')).value = daochu1.cell(i,column_index_from_string('C')).value
maxtongji_hang += 1
times = 0
file_value={}
for each in daochu1['C']:
if each.value == '10.254.221.140':
times+=1
print(times)
tongji.active['K2']=times
tongji.save('./waf策略统计.xlsx')
{:10_281:}{:10_281:} 这样的需求?你用个pandas读取excel,然后对IP这列isin(['10.254.221.140']),不就是筛选出这个IP的所有行了么?再count()数一下就知道个数了,也可以单独导出到excel,这不是很简单的么 疾风怪盗 发表于 2020-11-12 21:52
这样的需求?你用个pandas读取excel,然后对IP这列isin(['10.254.221.140']),不就是筛选出这个IP的所有行 ...
我是白中白我老老实实去看小甲鱼了{:5_109:} 本帖最后由 mb0221 于 2020-11-13 10:00 编辑
逃兵 发表于 2020-11-12 17:44
下班了,明天再说
大佬太厉害了,再来看看我新的帖子呗{:5_109:} 感谢感谢
页:
[1]