当前位置:网站首页>(混更一篇)多个txt文本转一个表格
(混更一篇)多个txt文本转一个表格
2022-07-01 12:14:00 【不爱打代码的程序员】
import xlwt
import os
xls = xlwt.Workbook()
def sort_fun(x):
return len(x)
def txt_xls_v2(filepath, xlspath, sort=True):
""" 多个txt文本转一个xls :param filename: txt文本路径 :param xlsname: xls路径 :return: """
try:
filename = os.path.basename(filepath).replace("-语料-url.txt", "")
f = open(filepath, 'r', encoding='utf-8')
lines = f.readlines()
if sort:
lines.sort(key=sort_fun)
sheet = xls.add_sheet(filename, cell_overwrite_ok=True)
for index, line in enumerate(lines):
# 这里按需写
# 按行循环,读取文本文件
# for i in range(len(line.split('\t'))):
# item = line.split('\t')[i]
# sheet.write(x, i, item)
item = line.split('\t')[-1]
sheet.write(index, 0, item)
f.close()
xls.save(xlspath) # 保存xls文件
except:
raise
if __name__ == "__main__":
dir_path = r"G:\20220623\测试"
xlsname = os.path.join(dir_path, os.path.basename(dir_path) + ".xls") # 多个txt保存到一个xls保存及命名
for root, dirs, files in os.walk(dir_path):
for file in files:
if file.endswith(".txt"):
file_path = os.path.join(root, file)
txt_xls_v2(file_path, xlsname)
边栏推荐
- Indefinite integral
- Message queue monitoring refund task batch process
- Typora adds watermarks to automatically uploaded pictures
- Seckill system 03 - redis cache and distributed lock
- Onenet Internet of things platform - the console sends commands to mqtt product devices
- Consolidate -c operator
- USB peripheral driver - cable connect/disconnect
- JPA and criteria API - select only specific columns - JPA & criteria API - select only specific columns
- Custom grpc plug-in
- LeetCode力扣(剑指offer 31-35)31. 栈的压入弹出序列32I.II.III.从上到下打印二叉树33. 二叉搜索树的后序遍历序列34. 二叉树中和为某一值的路径35. 复杂链表的复制
猜你喜欢

Talk about biological live broadcast - genovis Zhang Hongyan antibody specific enzyme digestion technology helps to characterize the structure of antibody drugs

邻接矩阵无向图(一) - 基本概念与C语言

【20211129】Jupyter Notebook远程服务器配置

Switch basic experiment

【20220605】文献翻译——虚拟现实中的可视化:一个系统的回顾

谈思生物直播—GENOVIS张洪妍抗体特异性酶切技术助力抗体药物结构表征
![[20211129] configuration du serveur distant du carnet de notes jupyter](/img/7c/79c9fcb91bde75e954dc3ecf9f5afd.png)
[20211129] configuration du serveur distant du carnet de notes jupyter

研发效能度量框架解读

本科毕业四年:工作,辞职,结婚,买房

Onenet Internet of things platform - create mqtts products and devices
随机推荐
Onenet Internet of things platform - create mqtts products and devices
[speech signal processing] 3 speech signal visualization -- prosody
Prepare for the Blue Bridge Cup Day10__ PWM control light brightness
usb peripheral 驱动 - cable connect/disconnect
Redis' attack tactics
自定義 grpc 插件
[Yunju entrepreneurial foundation notes] Chapter 7 Entrepreneurial Resource test 8
【datawhale202206】pyTorch推荐系统:召回模型 DSSM&YoutubeDNN
C knowledge point form summary 2
Emotion analysis based on IMDB comment data set
The Missing Semester
STM32 project practice (1) introduction and use of photosensitive resistor
LeetCode 454. Add four numbers II
uniapp 使用 uni-upgrade-center
The Missing Semester
Explore the contour detection function findcontours() of OpenCV in detail with practical examples, and thoroughly understand the real role and meaning of each parameter and mode
AI抠图工具
Theoretical basis of graph
S7-1500plc simulation
Understanding of MVVM and MVC