Tag: Port begins Excel lis pen file rom int parameter call
In order to automatically convert Excel data to the required Erlang data, listening to colleagues say that using Python is easy and easy, self-taught for two days Python, wrote a more coarse Python script, but can use, what optimization of the place please advise
The code is as follows:
#!/usr/bin/env python#-*-coding:utf-8-*-ImportSYS fromOpenpyxl.reader.excelImportLoad_workbookImportOSImportOs.pathdefgen_data (filename): WB= Load_workbook ('dataxlsx/'+ filename +'. xlsx')#load all pages of a fileSheetnames= Wb.get_sheet_names ()#get a list of all the paging namesWS = Wb.get_sheet_by_name (Sheetnames[0])#take the first page of paging data#print ' WS: ', WS#print "Work Sheet titile:", Ws.title # page name#print "Work Sheet rows:", Ws.max_row # Number of page rows#print "Work Sheet Cols:", Ws.max_column # Number of page columnscontent= []#Data ContentId_list = []#ID List#===============================================start concat Need data======================================== ========Content.append ('Percent of this file is auto maked!\n') Content.append ('-module ('+ filename +'). \ n') Content.append ('-compile (export_all). \ n') forIinchRange (4, Ws.max_row + 1):#read from the third row of the table, since the range function does not contain the end of the file, it takes +1 to read the last line forJinchRange (ws.max_column):ifWs[i][j].value = =None:content.append (' ,""') elifj = =0:id_list.append (int (ws[i][j].value)) Content.append ('Get ('+ str (ws[i][j].value). Strip () +')->\n') Content.append ('{r_'+ filename +', '+str (ws[i][j].value). Strip ())Else: Content.append (' ,'+str (ws[i][j].value). Strip ()) Content.append ('};\n') Content.append ('Get (_)->\n') Content.append ('not_match.\n') Content.append ('length ()->\n') Content.append (' '+ STR (ws.max_row-1) +'. \ n') Content.append ('id_list ()->\n'+ STR (id_list) +'.')#=====================================================end=================================================== ========== #Write Dataf = File ('./server/'+ filename +'. Erl','w+') f.writelines (content)Print 'Create new file:', filename +'. Erl'f.close ()#Close Channel returndefStart_gen ():#Delete old dataDelnames = Os.listdir ('./server') forDelnameinchDelnames:os.remove ('./server/'+delname)Print 'Delete old file:', Delname for_, _, filenamesinchOs.walk ('./dataxlsx'):#Traverse Folder forFileNameinchFilenames#traversing FilesFind = Filename.find ('. xlsx')#returns the file name length#print "Find is:", find ifFilename[0] = ='~' orfind = =-1:#The file name starts with ' ~ ' or cannot find the file name, as in '. ' The file that starts with Continue Else: Split_list= Filename.split ('.')#use '. ' Split file name, get [filename, file format]#Print Split_listGen_data (Split_list[0])#call Gen_data with a filename as a parameterStart_gen ()
Where the path is not compatible, the path must be correct
Use a Python script to read an Excel table to generate Erlang data