python read Excel write.txt method

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

Because today we need to write the data from Excel to the.txt file, we simply write the following code:


import numpy as np 
import xlrd # Open the excel file  
data= xlrd.open_workbook('./sudata/ng.xls')# Open the Excel File read data  
sh=data.sheet_by_name("Sheet1")## Get by workbook name  
print sh.nrows# The number of rows  5820 
print sh.ncols# The number of columns  2 
n=0 
i=0 
file=open("ng.txt","w") 
for n in range(sh.nrows): 
  for i in range(sh.ncols): 
    text=sh.cell_value(n,i).encode('utf-8') 
    file.write(text)  
    file.write('\n')  

Related articles: