python reads each row of data in txt and saves it to the instance in excel

  • 2020-08-22 22:12:38
  • OfStack

Use xlwt to read the contents of the txt file and write it to excel as follows, annotated.

The code is simple. The specific code is as follows:


# coding=utf-8
'''
main function : Mainly to achieve txt Writes each line of data to excel In the 
'''
#################
# The first 1 The code executed for the second time 
import xlwt # Written to the file 
import xlrd # Open the excel file 
fopen=open("e:\\a\\bb\\a.txt",'r')
lines=fopen.readlines()
# new 1 a excel file 
file=xlwt.Workbook(encoding='utf-8',style_compression=0)
# new 1 a sheet
sheet=file.add_sheet('data')
############################
# Write to write a.txt . a.txt The file has 20000 Line of the file 
i=0
for line in lines:
	sheet.write(i,0,line)
	i=i+1
#################################
# The first 2 The layer executes the code and writes b.txt . 
j=20001 # from 20001 Line of writing 
fopen2=open("e:\\a\\bb\\b.txt",'r')
lines2=fopen2.readlines()
for line in lines2:
	sheet.write(j,0,line)
	j=j+1
file.save('minni.xls')

Related articles: