Export the contents of each column of an Excel file to a TXT file, with the TXT file named Excel column name.
1 #!/usr/bin/python2 #-*-coding:utf-8-*-3 ImportXlrd#Import xlrd Module4 5 6 defIs_number (s):#determine whether a number7 Try:8 float (s)9 returnTrue#is the number returns trueTen exceptValueError: One Pass A - defMain (): -Workbook = Xlrd.open_workbook (Excel_path)#Open xlsx file theSheet =workbook.sheet_by_index (0) -Ncols = Sheet.ncols#get the number of worksheet columns - forIinchRange (0,ncols): -L1 = sheet.col_values (i)#store each column of data in a list +Name = Sheet.cell (0,i). Value#Get column name - ifIs_number (name) = = True:#determine if name is a number +txt = open (Txt_path + str (name) +'. txt','W')#Create a new text file A Else: attxt = open (Txt_path + name +'. txt','W') - while "' inchL1:#list elements Go empty -L1.remove ("') -L2 = l1[1:]#To intercept a list starting with the second element -New_list = List (set (L2))#filter repeating elements with collections -New_list.sort (key = L2.index)#sort new list elements by the original list elements in forQinchNew_list:#writes an element from a list to a text file - ifIs_number (q) = = True:#determines whether an element in a list is a number toTxt.write (str (q) +'\ n') + Else: -Txt.write (Q.encode ('Utf-8') +'\ n') theTxt.close ()#close a text file * if __name__=='__main__': $Excel_path = Raw_input (Unicode ('Please enter the Excel file path:','Utf-8'). Encode ('GBK'))#Enter the Excel file pathPanax NotoginsengTxt_path = Raw_input (Unicode ('Please enter the TXT file save path:','Utf-8'). Encode ('GBK'))#Enter TXT file to save directory -Main ()
Python script--excel file export by column