美文网首页
excel转有道词典xml (python3)

excel转有道词典xml (python3)

作者: LkSatan | 来源:发表于2020-02-25 21:22 被阅读0次

两步:

  1. 将excel文件转换为txt文件,好处理数据
  2. 将txt 转化为 xml

1

excel2txt.py

# -*- coding: utf-8 -*-
"""
Created on Tue Feb 25 20:04:43 2020

@author: cqq19
"""

import pandas as pd

df = pd.read_excel('D:\Python\code\workfile.xlsx', sheetname='Sheet1', header=None)     # 使用pandas模块读取数据
print('开始写入txt文件...')
df.to_csv('file1.txt', header=None, sep=',', index=False)       # 写入,逗号分隔
print('文件写入成功!')

2

excel2xml.py

# -*- coding: utf-8 -*-
"""
Created on Tue Feb 25 20:10:12 2020

@author: cqq19
"""

f = open("file1.txt","r")
fp = open("test.xml", "w")

content = f.readlines()
fp.write('<wordbook>')
i=1
for temp in content:
    fp.write("<item>")
    fp.write("<word>")
    fp.write(temp)
    i+=1
    fp.write("</word>")
    fp.write("<trans><![CDATA[ ]]></trans><phonetic><![CDATA[[ ]]]></phonetic><tags></tags><progress>1</progress>")
    fp.write("</item>")
    
fp.write('</wordbook>')
f.close
fp.close

3

导入单词完毕


image.png

相关文章

网友评论

      本文标题:excel转有道词典xml (python3)

      本文链接:https://www.haomeiwen.com/subject/sdeqchtx.html