51学通信论坛2017新版
标题:
txt转excel
[打印本页]
作者:
admin
时间:
2019-6-29 22:48
标题:
txt转excel
txt转excel,将txt中的数据以,为分隔符,转成excel文件。
代码:
from openpyxl import Workbook
def main(txtFileName):
new_XlsxFileName = txtFileName[:-3] + 'xlsx'
wb = Workbook()
ws = wb.worksheets[0]
with open(txtFileName) as fp:
for line in fp:
line = line.strip().split(',')
ws.append(line)
wb.save(new_XlsxFileName)
main('sample.txt')
复制代码
执行前的sample.txt:
3, 4, 2, 1
7, 8, 4, 2
12, 45, 6, 7
8, 9, 0, 5
81, 2, 3, 1
执行后的sample.xlsx文件:
[attach]5821[/attach]
欢迎光临 51学通信论坛2017新版 (http://bbs.51xuetongxin.com/)
Powered by Discuz! X3