有道词典生词本到excel的装换

本文介绍了一种将生词本数据从文本文件中读取,并通过编码转换后,利用Python的xlwt库将其写入Excel文件的方法。具体步骤包括解析文本文件中的生词、音标和释义,然后设置Excel表格样式并写入数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

生词本的导出

编码转换并且写入excel

import codecs
import xlwt

intlist = ['1','2','3','4','5','6','7','8','9','0']
worddict = {}
#{'word':[word,soundmark,[mean]]}

meanlist_ = []
meanlist = []
with open('./zzq.txt','r',encoding='utf-16') as wordfile:
    while True :
        wordline = wordfile.readline()
        if(wordline):
            if(wordline[0] in intlist):
                word = wordline.split('[')[0].split(',')[1].strip()
                if len(wordline.split('['))>3:
                    soundmark = ''
                    for soundmark_ in wordline.split('[')[1:]:
                        soundmark = soundmark +'['+soundmark_.strip()
                elif len(wordline.split('['))>1:
                    soundmark = '['+wordline.split('[')[1].strip()
                else:
                    soundmark = ''
                worddict[word]=[word,soundmark,[]]
            else:
                if '人名' not in wordline:
                    worddict[word][2].append(wordline)  
                else:
                    pass       
        else:
            break


#设置表格样式
def set_style(name,height,bold=False):
    style = xlwt.XFStyle()
    font = xlwt.Font()
    font.name = name
    font.bold = bold
    font.color_index = 4
    font.height = height
    style.font = font
    return style

# 写Excel
def write_excel(worddict):
    f = xlwt.Workbook()
    sheet1 = f.add_sheet('words',cell_overwrite_ok=True)
    for item,word in enumerate(  worddict):
        sheet1.write(item,0,worddict[word][0],set_style('Times New Roman',220,True))
        sheet1.write(item,1,worddict[word][1],set_style('Times New Roman',220,True))
        for item2,meaning in enumerate(  worddict[word][2]):
            sheet1.write(item,2+item2,meaning,set_style('Times New Roman',220,True))
    f.save('word.xls')


write_excel(worddict)

结果

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值