import openpyxl
txtname = 'C:/Users/123/Desktop/SRRLdata/parameter.txt'#txt路径
excelname = 'C:/Users/123/Desktop/parameter.xlsx'#要保存的excel路径
fopen = open(txtname, 'r')
lines = fopen.readlines()
wb=openpyxl.Workbook()#创建1个工作簿
ws=wb.create_sheet(u'Data_L')#用工作簿去创建工作表sheet
i = 1#从第一行开始
l=len(lines)
for j in range(0,l):
line=lines[j]
line = line.strip('\n')
line = line.split(',')#txt列与列通过','分隔
s = line[0]#有几列就建立几个line
a = line[1]
b = line[2]
c = line[3]
d = line[4]
e = line[5]
ws.cell(i, 1,s)
ws.cell(i,2, a)
ws.cell(i, 3, b)
ws.cell(i, 4,c)
ws.cell(i, 5, d)
ws.cell(i, 6, e)
i = i + 1
wb.save(excelname)#保存文件
python将txt中的内容导入到excel
最新推荐文章于 2023-12-23 17:11:37 发布