当前位置:网站首页>(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)
边栏推荐
- Consolidate -c operator
- Computer graduation project asp Net hotel room management system VS development SQLSERVER database web structure c programming computer web page source code project
- 队列的链式存储
- Rural guys earn from more than 2000 a month to hundreds of thousands a year. Most brick movers can walk my way ǃ
- Onenet Internet of things platform - the console sends commands to mqtt product devices
- Golang des-cbc
- Personnaliser le plug - in GRPC
- Unity XLua 协程封装
- LeetCode 454. Add four numbers II
- 基于IMDB评论数据集的情感分析
猜你喜欢

C knowledge point form summary 2

自组织是管理者和成员的双向奔赴

uniapp 使用 uni-upgrade-center

Switch basic experiment

Computer graduation project asp Net hotel room management system VS development SQLSERVER database web structure c programming computer web page source code project
![[20211129] jupyter notebook remote server configuration](/img/7c/79c9fcb91bde75e954dc3ecf9f5afd.png)
[20211129] jupyter notebook remote server configuration

On recursion and Fibonacci sequence

Indefinite integral

Uniapp uses uni upgrade Center
![[some notes]](/img/91/7657f90b50f012736579b1585b4ade.jpg)
[some notes]
随机推荐
Custom grpc plug-in
被锡膏坑了一把
Arm GIC (V) how arm TrustZone supports security interrupt analysis notes.
双链表有关操作
On recursion and Fibonacci sequence
MySQL data table creation
The Missing Semester
I'm in Zhongshan. Where is a better place to open an account? Is it actually safe to open an account online?
巩固-C#运算符
[20211129] configuration du serveur distant du carnet de notes jupyter
[MCU] [nixie tube] nixie tube display
Botu V15 add GSD file
Implementation of address book management system with C language
技术分享 | MySQL:从库复制半个事务会怎么样?
【datawhale202206】pyTorch推荐系统:多任务学习 ESMM&MMOE
Blue Bridge Cup multi interface switching processing (enumeration plus state machine method)
Deep understanding of grpc part1
【20220605】文献翻译——虚拟现实中的可视化:一个系统的回顾
LeetCode 454. Add four numbers II
Joint Time-Frequency and Time Domain Learning for Speech Enhancement