成都网站建设设计

将想法与焦点和您一起共享

Python利用xlwt模块实现将数据写入excel文件-创新互联

Python 利用xlwt模块实现将数据写入excel文件?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

创新互联建站主营桓台网站建设的网络公司,主营网站建设方案,手机APP定制开发,桓台h5小程序定制开发搭建,桓台网站营销推广欢迎桓台等地区企业咨询
#!/usr/bin/python
# -*- coding: utf-8 -*-

import xlwt
import re

def host_regex(dataline):
 host_regex = r"(.*?)"
 host = re.findall(host_regex, dataline)
 if host:
  return host[0]

def ip_regex(dataline):
 ip_regex = r"(.*?)"
 ip = re.findall(ip_regex, dataline)
 if ip:
  return ip[0]

with open("zbx_export_hosts.xml", "r", encoding="utf8") as f:
 lines = f.readlines()
 alldatas = []
 hostlist = []
 iplist = []
 for line in lines:
  host = host_regex(line)
  ip = ip_regex(line)

  if host is not None:
   hostlist.append(host)
  if ip is not None:
   iplist.append(ip)
 hosts_ip = []
 # 构造数据结构,形如:[[a,b],[c,d],...]
 multi_list = map(list, zip(hostlist, iplist))
 for multi in multi_list:
  hosts_ip.append(multi)
 workbook = xlwt.Workbook()
 worksheet = workbook.add_sheet('test')
 # 关键代码
 for hi in enumerate(hosts_ip):
  for num in range(len(hi[1])):
   # 行、列、值
   worksheet.write(hi[0], num, hi[1][num])

 workbook.save('excelwrite.xls')

文章题目:Python利用xlwt模块实现将数据写入excel文件-创新互联
本文来源:http://chengdu.cdxwcx.cn/article/hoisi.html