为了对excel逐行逐列进行xml的转换,从读取excel文件到生成xml文件。
import pandas
import xml.dom.minidom
# 在内存中创建一个空的文档,还未真实写入
doc = xml.dom.minidom.Document()
# 创建一个根节点Datas对象
root = doc.createElement('Datas')
# 为节点添加属性方法
root.setAttribute('university', 'xx理工大学')
root.setAttribute('address', 'xx科技园')
# 将root作为子节点加入
doc.appendChild(root)
# excel文件位置
path = r"D:\Desktop\Dev.xls"
try:
# 使用pandas读取, 修改读取的最大长度为512(过长会以...省略)
pandas.set_option('max_colwidth', 512)
sheet = pandas.read_excel(path)
# sheet.index.values 共多少行
# 逐行读取,并进行逐列操作
for row in sheet.index.values:
dic = dict()
dic['text'] = sheet.iloc[row, 0] # 文本
dic['label'] = sheet.iloc[row, 1] # 标签
text = str(dic['text']).strip().replace(' ', '')