Python 3 | txt转Excel | 用拼音+数字对各个数据元编码 | 读取多个文件夹内容 | import xlwt + xpinyin + os

Python 3 | txt转Excel | 用拼音+数字对各个数据元编码 | 读取多个文件夹内容 | import xlwt + xpinyin + os

我在研零入学,倍受可爱师姐欺负,各种梳理表格的杂活,所以有此博客。
我事先把word里的数据复制出来放在了txt中。有多个文件夹,每个文件夹中有多个txt。
1、由于之前导入doc文件不会,所以用来txt;doc转docx在下篇博客中有;
2、研究生杂活多怎么办?写代码啊!
3、python读取txt文档
4、python编写excel
5、python读取多个文档内容

姊妹篇

python 3| docx的读写 | excel的读 | word行中添加新的key-value - 我是一块小石头 - CSDN博客
https://blog.csdn.net/stone_fall/article/details/96428915
python 3 | doc转docx - 我是一块小石头 - CSDN博客
https://blog.csdn.net/stone_fall/article/details/96428876
Python 3 | txt转Excel | 用拼音+数字对各个数据元编码 | 读取多个文件夹内容 | import xlwt + xpinyin + os - 我是一块小石头 - CSDN博客
https://blog.csdn.net/stone_fall/article/details/96428734

数据格式

4.1 xxxxx
key1:value
key2:。。
key3:。
key4:。
key5:。
key6:。
key7:。
key8:。
key9:。
key10:。
-----------------------------------------------------
key1:value
key2:。。
key3:。
key4:。
key5:。
key6:。
key7:。
key8:。
key9:。
key10:。
-----------------------------------------------------
……

python3代码如下

#coding:utf-8
import os
import xlwt
import xlrd
from xpinyin import Pinyin
#路径
original_data_path = 'xxxxxxxxx\\original\\'
final_data_path = 'xxxxxxxxx\\final\\'
# 读取文件夹
dirs = os.listdir(original_data_path)
#字典,用于统计各个txt中的数据元
myDict = dict()
for file_dir in dirs:
    tmp_dir = os.path.join(original_data_path,file_dir)
    if os.path.isdir(tmp_dir):
        print(tmp_dir)
        txt_dirs = os.listdir(tmp_dir)
        result_dir = os.path.join(final_data_path,file_dir)
        print(result_dir)
        if not os.path.exists(result_dir):
            os.mkdir(result_dir)
        for txt_name in txt_dirs:
            tmp_txt_dir = os.path.join(tmp_dir,txt_name)
            print(tmp_txt_dir)
            excel_name = os.path.basename(tmp_txt_dir).split('.')[0] + '.xls'
            # print(excel_name)
            excel_dir = os.path.join(final_data_path,file_dir,excel_name)
            print(excel_dir)

            # 新建一个excel文件
            file = xlwt.Workbook(encoding='utf-8', style_compression=0)
            # 新建一个sheet
            sheet = file.add_sheet('data')
            fopen = open(tmp_txt_dir, 'r')
            lines = fopen.readlines()
            count = 11 #我一个数据元有11个属性
            p = Pinyin()
			
			#表格的表头属性
            sheet.write(0, 0, '1')
            sheet.write(0, 1, '2')
            sheet.write(0, 2, '3')
            sheet.write(0, 3, '4')
            sheet.write(0, 4, '5')
            sheet.write(0, 5, '6')
            sheet.write(0, 6, '7')
            sheet.write(0, 7, '8')
            sheet.write(0, 8, '9')
            sheet.write(0, 9, '10')
            sheet.write(0, 10, '11')
			
			#读取txt中各行内容
            for line in lines:
                # Python strip() 方法用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列。
                line = line.strip('\n')
                line = line.strip()
                if len(line) == 0:
                    continue
                line = line.split(':', 1)
                # print(count, ' ', line[1:])
                if len(line) == 1:
                    continue
                # print(count,' ',line)
                if count % 11 == 2:
                    count = count + 1
                i = count % 11
                j = count // 11
                value = ''.join(line[1:])
                sheet.write(j, i, value)
                if i == 0:
                    if value in myDict:
                        print('value in myDict, value is %s' % value)
                        num = myDict[value] + 1
                        myDict[value] = num
                    else:
                        num = 1
                        myDict[value] = num
                    tmp = p.get_initials(value, u'')
                    tmp = tmp + str(num).zfill(2)
                    # sheet.write(j, i + 1, tmp)
                    # count = count + 1
                    sheet.write(j, i+2, tmp)
                count = count + 1

            file.save(excel_dir)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值