|
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文件:
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
|