Learn the use of related modules and function methods with the following code
#Coding:utf-8#Import the appropriate moduleImportCSVImportXLWTImportSYSImportOSImportFnmatch#Save as file namedefEx_file (mycsvfile): CSVFile= Open (Mycsvfile,"RB") #csvfile = open ("Test.csv", "RB") #Create a new Excel fileMyexcel =XLWT. Workbook ()#New Sheet pageMySheet = Myexcel.add_sheet ("Data") #get CSV file absolute path and file name does not include suffix,
and Os.path.splitext ("absolute path to File")
Returned is a list of two elements,
PORTION[0] is the value that contains the file name, Portion[1] is ". suffix"Portion =Os.path.splitext (mycsvfile)#Read file informationReader =Csv.reader (csvfile) L=0#get a single line of information through loops forLineinchReader:r=0#get single cell information through a double loop forIinchLine :#write Excel tables by double loopMysheet.write (l,r,i) R=r+1L=l+1#last saved to Excel, save with original file name, replace suffixMyexcel.save (portion[0]+". xls") PrintPortion[0] +'. xls'+u"Save success! " #file path processing functiondefiterfindfiles (Path, fnexp):#The Os.walk () function returns three functions, and root is the path itself and the path of all folders below it.
Dir is the folder name under Path, not an empty list [],files is a collection of file lists under path and folder under Path #this is equivalent to traversing all the files under the path, which can be viewed by print their return values forRoot, dirs, filesinchOs.walk (path):#The fnmatch.filter (,) return value is the file list information that gets the suffix of fnexp.
That is, a new list of files that match the suffix of the fnexp parameter value in the file list that contains the suffix is assigned to the filename. forFileNameinchfnmatch.filter (Files, fnexp):#Root is the path of the current traversal, where the Os.path.join () method is the root path and filename concatenation as the absolute path to the file #yield is the iterative meaning of appending data to the return value of the function Iterfindfiles (), returning a return value each time the loop returns yieldos.path.join (root, filename)#Batch processingif __name__=="__main__": #enter the path of the file you want to convertPath=raw_input ("Please enter a path:") #The file name of the data to be converted is batch, so use the wildcard "*" number "*.csv" to represent all CSV format files under the target pathFnexp=raw_input ("Please enter a fnexp:") #invoking a file handler function forFileNameinchiterfindfiles (Path, fnexp): #filename is to receive a list of return values, return a value to call the Ex_file () function, and return the value as a parameter ex_file (filename) raw _input ('Please enter to exit')
Description of the function used in the code
1, Os.walk (path)
Import OS for in Os.walk (R'D:\testDemoCode'): print root # The root of the directory that is currently traversed print dirs #print files # All filenames under the root of the currently traversed directory include suffixes, which are stored in the list
2, Fnmatch.filter (Files, fnexp)
Match the files in the files file list with the suffix fnexp and return the matching results to a list
3. Os.path.join (root, filename)
Join the file name filename filename with the path root as a whole
4. Yield
Yield is equivalent to an iterator that returns a return value to the main function
5. Os.path.splitext (mycsvfile) && os.path.split ("file name")
The absolute path and extension of the exploded file is a list []
Fpathandname, fext = Os.path.splitext ("The path you want to explode")
For example:
A, B = os.path.splitext ("C:\\123\\456\\test.txt")
Print a
Print B
Show:
C:\123\456\test
. txt
Decompose a pathname into two parts, directory name and file name
Fpath, fname = Os.path.split ("The path you want to explode")
For example:
A, B = os.path.split ("C:\\123\\456\\test.txt")
Print a
Print B
Show:
c:\123\456
Test.txt
Bulk processing CSV format converted to XLS