python:解析XML文件后写入EXCEL(二)

之前写过一个类似的,现在又要将一个xml文件转换为excel,这个比上次要容易,因为xml文件的结构很简单。

EXCEL效果:




原XML文件:



代码:

import re
import xlwt

f=open("partner_id.xml","r")
content=f.read()
f.close()

book=xlwt.Workbook()
sheet1=book.add_sheet("hi",cell_overwrite_ok=True)
tall_style=xlwt.easyxf("font:height 400")
title=["包名","渠道"]
for i in title:
    sheet1.write(0,title.index(i),i)

channelList=re.findall("<PathDetail.*?</PathDetail>",content,re.S)
for j in channelList:
    pname=re.findall("<pname>(.*?)</pname>",j,re.S)[0]
    channel=re.findall("<pid>(.*?)</pid>",j,re.S)[0]
    sheet1.write(channelList.index(j)+1,0,pname)
    sheet1.write(channelList.index(j)+1,1,channel)

col0=sheet1.col(0)
col1=sheet1.col(1)
row0=sheet1.row(0)
col0.width=256*30
col1.width=256*20
row0.set_style(tall_style)

book.save("channel_id.xls")




发布了23 篇原创文章 · 获赞 5 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/CatherineC20150619/article/details/51525716
今日推荐