自动将转换txt转换为xls [英] Automate conversion txt to xls

查看:123
本文介绍了自动将转换txt转换为xls的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找将文件夹结构中所有文本文件(制表符分隔)转换为.xls格式的最便宜的方式,保持列和行的形状。

I am looking for the cheapest way of automating the conversion of all the text files (tab-delimited) in a folder structure into .xls format, keeping the shape of columns and rows as it is.

目前我在MacOS上。 Linux和Windows可用。

Currently I am on MacOS. Linux and Windows are available though.

编辑:

import xlwt
import xlrd
f = open('Text.txt', 'r+')
row_list = []
for row in f:
    row_list.append(row.split())
column_list = zip(*row_list)
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('Sheet1')
i = 0 
for column in column_list:
    for item in range(len(column)):
        worksheet.write(item, i, column[item])
    workbook.save('Excel.xls')
    i+=1

这应该做...

推荐答案

最简单的方法是将所有文件从* .txt重命名为* .xls。 Excel将自动分割数据,保持原始形状。

The easiest way would be to just rename all of the files from *.txt to *.xls. Excel will automatically partition the data, keeping the original shape.

我不会为您编写代码,但这里是一个开始:

I'm not going to write your code for you, but here is a head start:

  • You can list a directories contents using os.listdir()
  • You can use os.path.isdir() and os.path.isfile() to see if each 'thing' you just found in your intial directory is a file or a directory, and act on accordingly
  • You can use os.rename() to rename a file and os.remove() to delete a file
  • You can use os.path.splitext() to split the files name and extension, or just file.endswith('.txt') to work on only the correct files

这篇关于自动将转换txt转换为xls的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆