python & excel
#!/usr/bin/python
# coding: utf-8
import os
import re
import sys
import xlrd
SERVERS_EXCELFILE = u'/etc/subversion/平台服/平台服务器资料.xls'
TOP_DIR = os.path.dirname(os.path.abspath(sys.argv[0]))
HOSTFILE = os.path.join(TOP_DIR,'hosts')
serverlist = []
sheets = {
u"平台网站":[1,3,5,6,7],
#u"平台接口":[1,2,5,6,9],
}
def sheet_parse(excelfile,sheetname,rols):
global serverlist
sheet = excelfile.sheet_by_name(sheetname)
for row in range(rols[0],sheet.nrows):
wan_ip = sheet.cell_value(row,rols[1])
lan_ip = sheet.cell_value(row,rols[2])
if not re.match(r'^[1-9]{0,2}\.[0-9]{1,3}',wan_ip):
wan_ip = 0
if not re.match(r'^[1-9]{0,2}\.[0-9]{1,3}',lan_ip):
lan_ip = 0
if wan_ip or lan_ip:
server = '%s %s %d %s\n' % (
str(wan_ip).split()[0],
str(lan_ip).split()[0],
int(sheet.cell_value(row,rols[3])),
sheet.cell_value(row,rols[4])
)
serverlist.append(server)
def main():
excelfile = xlrd.open_workbook(SERVERS_EXCELFILE)
if os.path.exists(HOSTFILE):
os.remove(HOSTFILE)
for sheetname in sheets.keys():
sheet_parse(excelfile,sheetname,sheets[sheetname])
try:
hostsfile = open(HOSTFILE,'w')
hostsfile.writelines(serverlist)
finally:
hostsfile.close()
if __name__ == '__main__':
main()
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341