Python 利用xlwt模塊實現(xiàn)將數(shù)據(jù)寫入excel文件?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
創(chuàng)新互聯(lián)建站主營桓臺網(wǎng)站建設的網(wǎng)絡公司,主營網(wǎng)站建設方案,手機APP定制開發(fā),桓臺h5小程序定制開發(fā)搭建,桓臺網(wǎng)站營銷推廣歡迎桓臺等地區(qū)企業(yè)咨詢#!/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 = [] # 構造數(shù)據(jù)結構,形如:[[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')