Use a Python script to read an Excel table to generate Erlang data

Source: Internet
Author: User
Tags python script

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

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.