当前位置:网站首页>(mixed version 1) multiple TXT text to one table
(mixed version 1) multiple TXT text to one table
2022-07-01 12:15:00 【Programmers who don't like coding】
import xlwt
import os
xls = xlwt.Workbook()
def sort_fun(x):
return len(x)
def txt_xls_v2(filepath, xlspath, sort=True):
""" Multiple txt Text to one xls :param filename: txt Text path :param xlsname: xls route :return: """
try:
filename = os.path.basename(filepath).replace("- corpus -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):
# Write here as needed
# Press the line to cycle , Read text file
# 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) # preservation xls file
except:
raise
if __name__ == "__main__":
dir_path = r"G:\20220623\ test "
xlsname = os.path.join(dir_path, os.path.basename(dir_path) + ".xls") # Multiple txt Save to a xls Preservation and naming
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)
边栏推荐
- Golang根据参数引入相应配置文件的实现方式
- ANSI/UL 94 VTM薄质材料垂直燃烧测试
- GID: open vision proposes a comprehensive detection model knowledge distillation | CVPR 2021
- (混更一篇)多个txt文本转一个表格
- [Yunju entrepreneurial foundation notes] Chapter 7 Entrepreneurial Resource test 6
- [Yunju entrepreneurial foundation notes] Chapter 7 Entrepreneurial Resource test 5
- 区间乘积的因子数之和——前缀和思想+定一移二
- LeetCode力扣(剑指offer 31-35)31. 栈的压入弹出序列32I.II.III.从上到下打印二叉树33. 二叉搜索树的后序遍历序列34. 二叉树中和为某一值的路径35. 复杂链表的复制
- 栈的应用——括号匹配问题
- 双链表有关操作
猜你喜欢

C serialization simple experiment

Huawei HMS core joins hands with hypergraph to inject new momentum into 3D GIS

Onenet Internet of things platform - mqtt product devices send messages to message queues MQ

Typora realizes automatic uploading of picture pasting

MySQL workbench data modeling function

What are the PHP FPM configuration parameters

Use of easyexcel

被锡膏坑了一把

Powerful, easy-to-use, professional editor / notebook software suitable for programmers / software developers, comprehensive evaluation and comprehensive recommendation

【20211129】Jupyter Notebook遠程服務器配置
随机推荐
栈的应用——括号匹配问题
Switch basic experiment
Deep understanding of grpc part1
[Yunju entrepreneurial foundation notes] Chapter VII Entrepreneurial Resource test 1
二叉树的链式存储
Ipv6-6to4 experiment
[JS advanced] promise explanation
Joint Time-Frequency and Time Domain Learning for Speech Enhancement
IOS interview
What are the PHP FPM configuration parameters
Talk about biological live broadcast - genovis Zhang Hongyan antibody specific enzyme digestion technology helps to characterize the structure of antibody drugs
Neo4j Chinese developer monthly - issue 202206
Acly and metabolic diseases
[Yunju entrepreneurial foundation notes] Chapter 7 Entrepreneurial Resource test 5
usb peripheral 驱动 - cable connect/disconnect
Why does the JVM heap memory exceed 32g and pointer compression fail?
Talk about the pessimistic strategy that triggers full GC?
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
自定義 grpc 插件
NOV Schedule for . Net to display and organize appointments and recurring events