Python script--excel file export by column

Source: Internet
Author: User
Tags python script

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.