python xlsx转xls_使用python将tsv文件转换为xls / xlsx - IT屋-程序员软件开发技术分享社区...

I want to convert a file in tsv format to xls/xlsx..

I tried using

os.rename("sample.tsv","sample.xlsx")

But the file getting converted is corrupted. Is there any other method of doing it?

解决方案

Here is a simple example of converting TSV to XLSX using XlsxWriter and the core csv module:

import csv

from xlsxwriter.workbook import Workbook

# Add some command-line logic to read the file names.

tsv_file = 'sample.tsv'

xlsx_file = 'sample.xlsx'

# Create an XlsxWriter workbook object and add a worksheet.

workbook = Workbook(xlsx_file)

worksheet = workbook.add_worksheet()

# Create a TSV file reader.

tsv_reader = csv.reader(open(tsv_file, 'rb'), delimiter='\t')

# Read the row data from the TSV file and write it to the XLSX file.

for row, data in enumerate(tsv_reader):

worksheet.write_row(row, 0, data)

# Close the XLSX file.

workbook.close()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值