Python reads the excel table to generate erlang data, pythonerlang

Source: Internet
Author: User

Python reads the excel table to generate erlang data, pythonerlang

In order to automatically convert excel data into the required erlang data, I heard from my colleagues that it would be very convenient to use python. I learned python for two days and wrote a rough python script, however, it can be used. Please advise on any optimizations.

The Code is as follows:

#! /Usr/bin/env python #-*-coding: UTF-8-*-import sysfrom openpyxl. reader. excel import load_workbookimport osimport OS. pathdef gen_data (filename): wb = load_workbook ('dataxlsx/'+ filename + '.xlsx') # Load all file pages (sheetnames = wb. get_sheet_names () # Get the name list of all pages ws = wb. get_sheet_by_name (sheetnames [0]) # retrieve data from the first page # print 'ws: ', ws # print "Work Sheet Titile:", ws. title # Page name # print "Work Sheet Rows:", w S. max_row # Number of paging lines # print "Work Sheet Cols:", ws. max_column # Number of paging columns content = [] # data content id_list = [] # ID list #================== === start concat need data =========================== content. append ('% this file is auto maked! \ N') content. append ('-module (' + filename + '). \ n') content. append ('-compile (export_all ). \ n') for I in range (4, ws. max_row + 1): # Read from the third row of the table. Because the range function does not include the end of the file, + 1 for j in range (ws. max_column): if ws [I] [j]. value = None: content. append (', ""') elif j = 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 data f = file ('. /serv Er/'+ filename + '. erl', 'W + ') f. writelines (content) print 'create new file: ', filename + '. erl 'F. close () # close channel returndef start_gen (): # delete old data delnames = OS. listdir ('. /Server') for delname in delnames: OS. remove ('. /server/'+ delname) print 'delete old file:', delname for _, _, filenames in OS. walk ('. /dataxlsx'): # traverse the folder for filename in filenames: # traverse the file find = filename.find('.xlsx') # Return the length of the file name Degree # print "find is:", find if filename [0] = '~ 'Or find =-1: # the file name must be '~ 'Or the file name cannot be found, such '. 'File continue else: split_list = filename. split ('. ') # Use '. 'split the file name and obtain the [file name, file format] # print split_list gen_data (split_list [0]) # Call gen_datastart_gen () using the file name as the parameter ()

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.