python读excel示例

1,read-errno-cfg.py

# -*- coding: utf-8 -*-
'''
Created on 2011-6-25

@author: hgc
'''
from xlrd import open_workbook
from google.protobuf import text_format

# 添加module路径
import sys
sys.path.append("../proto/")

from errno_cfg_pb2 import ErrnoCfgSvrList, ErrnoCfgCliList

strErrnoCfgFile = "../excel/errno.xls"
strCfgCliOB = "../dat/cli/errno-cli.dat" # output binary
strCfgCliOT = "../dat/cli/errno-cli.txt" # output text
strCfgSvrOB = "../dat/svr/errno-svr.dat"
strCfgSvrOT = "../dat/svr/errno-svr.txt"

def CfgCliCmp(objCfg1, objCfg2):
    return objCfg1.u32Val - objCfg2.u32Val

def CfgSvrCmp(objCfg1, objCfg2):
    return objCfg1.u32Val - objCfg2.u32Val

if __name__ == '__main__':
    book = open_workbook(strErrnoCfgFile)
    sheet1 = book.sheet_by_index(1)
   
    objCfgCliList = ErrnoCfgCliList()
    objCfgSvrList = ErrnoCfgSvrList()
   
    for i in range(1, sheet1.nrows):
        if ("" == sheet1.cell_value(i, 0)): # 空行
            break;
       
        # cli
        objCfgCli = objCfgCliList.astErrnoCfg.add()
        objCfgCli.strName = sheet1.cell_value(i, 2)
        objCfgCli.u32Val = int(sheet1.cell_value(i, 3))
        objCfgCli.strDescZH = sheet1.cell_value(i, 4)
        objCfgCli.strDescEN = sheet1.cell_value(i, 5)
       
        # svr
        objCfgSvr = objCfgSvrList.astErrnoCfg.add()
        objCfgSvr.strName = sheet1.cell_value(i, 2)
        objCfgSvr.u32Val = int(sheet1.cell_value(i, 3))
        objCfgSvr.strDescZH = sheet1.cell_value(i, 4)
       
    # 排序
    objCfgCliList.astErrnoCfg.sort(CfgCliCmp)
    objCfgSvrList.astErrnoCfg.sort(CfgSvrCmp)
   
    # cli 输出二进制格式
    fCfgCliOB = open(strCfgCliOB, "wb")
    fCfgCliOB.write(objCfgCliList.SerializeToString())
    fCfgCliOB.close()
   
    # cli 输出可读格式
    fCfgCliOT = open(strCfgCliOT, "w")
    text_format.PrintMessage(objCfgCliList, fCfgCliOT, 0, True, False)
    fCfgCliOT.close()
   
    # svr 输出二进制格式
    fCfgSvrOB = open(strCfgSvrOB, "wb")
    fCfgSvrOB.write(objCfgSvrList.SerializeToString())
    fCfgSvrOB.close()
   
    # svr 输出可读格式
    fCfgSvrOT = open(strCfgSvrOT, "w")
    text_format.PrintMessage(objCfgSvrList, fCfgSvrOT, 0, True, False)
    fCfgSvrOT.close()

    print "done"

2,errno-cfg.proto

enum ErrnoNameBufSZ
{
 ERRNO_NAME_BUF_SZ = 48;
}

enum ErrnoDescBufSZ
{
 ERRNO_DESC_BUF_SZ = 128;
}

message ErrnoCfgSvr
{
 required string strName = 1;
 required uint32 u32Val = 2;
 required string strDescZH = 3;
}

message ErrnoCfgSvrList
{
    repeated ErrnoCfgSvr astErrnoCfg = 1;
}

message ErrnoCfgCli
{
 required string strName = 1;
 required uint32 u32Val = 2;
 required string strDescZH = 3;
 required string strDescEN = 4;
}

message ErrnoCfgCliList
{
    repeated ErrnoCfgCli astErrnoCfg = 1;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值