当前位置:网站首页>(混更一篇)多个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)
边栏推荐
- Le semester manquant
- 巩固-C#运算符
- Onenet Internet of things platform - create mqtts products and devices
- ANSI/UL 94 VTM薄质材料垂直燃烧测试
- Chen Gong: Micro service, is it still so pure?
- 研发效能度量框架解读
- 技术分享 | MySQL:从库复制半个事务会怎么样?
- Mysql database knowledge collation
- Leetcode (Sword finger offer) - 58 - ii Rotate string left
- ACLY与代谢性疾病
猜你喜欢
随机推荐
[Yunju entrepreneurial foundation notes] Chapter 7 Entrepreneurial Resource test 5
Message queue monitoring refund task batch process
leetcode 406. Queue reconstruction by height
指纹浏览器工作原理、使用场景以及重要性简单讲解
The Missing Semester
[106] 360 check font - check whether the copyright of local Fonts is commercially available
C knowledge point form summary 2
Consolidate -c operator
How does Nike dominate the list all the year round? Here comes the answer to the latest financial report
[Yu Yue education] financial management reference materials of Ningbo University of Finance and Economics
Computer graduation project asp Net attendance management system vs developing SQLSERVER database web structure c programming computer web page source code project
How to install php7 and perform performance test using yum
The Missing Semester
【datawhale202206】pyTorch推荐系统:召回模型 DSSM&YoutubeDNN
91.(cesium篇)cesium火箭发射模拟
构建外部模块(Building External Modules)
Computer graduation project asp Net hotel room management system VS development SQLSERVER database web structure c programming computer web page source code project
JPA and criteria API - select only specific columns - JPA & criteria API - select only specific columns
91. (cesium chapter) cesium rocket launch simulation
Unity xlua co process packaging




![[MCU] [nixie tube] nixie tube display](/img/5e/9e14302b4e4f5e03601392ac90479d.png)




