Skip to content

Instantly share code, notes, and snippets.

@tawateer
Created March 21, 2019 11:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tawateer/bc2f9f0108ea47cfe7f1d6c3a9ff1dee to your computer and use it in GitHub Desktop.
Save tawateer/bc2f9f0108ea47cfe7f1d6c3a9ff1dee to your computer and use it in GitHub Desktop.
写 excel 相关操作
#!/bin/env python
# -*- coding:utf-8 -*-
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
from IPy import IP
import xlwt
file_path = "ip_laiyuan.txt"
def process(ip, mask):
x = IP(ip).make_net(mask)
return str(x[0]), str(x[-1])
def get_data():
data = []
with open(file_path) as f:
for i in f:
i = i.decode('utf-8')
ip = i.strip().split()[0]
mask = i.strip().split()[1]
man = i.strip().split()[2]
x = process(ip, mask)
data.append([x[0], x[1], "IPv4", man])
return data
def write_data_to_excel(name):
data = get_data()
wbk = xlwt.Workbook()
sheet = wbk.add_sheet('Worksheet', cell_overwrite_ok=True)
first = [u"起始IP", u"终止IP", u"IP地址类型", u"来源单位"]
data.insert(0, first)
for i in xrange(len(data)):
for j in xrange(len(data[i])):
# print i
sheet.write(i, j, data[i][j])
wbk.save(name+'.xls')
def main():
write_data_to_excel("ip_laiyuan")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment