python-多语言功能-读excel文件并写入json,解决json输出unicode

import  xdrlib ,sys
import xlrd
import json
reload(sys)
sys.setdefaultencoding("utf-8")

TYPE_CH = 0
TYPE_EN = 1
TYPE_FR = 2


class LanUnit():
    def __init__(self):
        self.jVal = None
        self.id = 0
        self.fr = ''
        self.en = ''
        self.ch = ''

    def init_unit(self, id, jVal):
        self.jVal =jVal
        self.id = id
        try:
            self.fr = jVal['fr']
            self.en = jVal['en']
            self.ch = jVal['ch']
        except:
            pass

    def is_cur_unit(self, id):
        return id == self.id

    def get_FR(self):
        return  self.fr
    def get_EN(self):
        return  self.en
    def get_CH(self):
        return  self.ch


class LanPool():
    def __init__(self):
        self.lan_dict = {}

    @classmethod
    def reload_config(self):
        self.init_map_from_json()

    def init_map_from_json(self):
        '''
        self.raw_config = {
            "1001": {
                "fr":"fr",
                "en":"en",
                "ch":"ch"
            },
            "1002": {
                "fr": "fr",
                "en": "en",
                "ch": "ch"
            }
        }
        '''
        self.raw_config = {
            "1001": {
                "fr": "fr",
                "en": "en",
                "ch": "ch"
            },
            "1002": {
                "fr": "fr",
                "en": "en",
                "ch": "ch"
            }
        }

        for rc in self.raw_config:
            jVal = self.raw_config[rc]
            lu = LanUnit()
            lu.init_unit(rc)
            self.lan_dict[rc] = lu

    def has_key(self, id):
        return hasattr(self.lan_dict, id)

    def get_str(self, id, type):
        ID_ERROR = 'ID_ERROR(%d)'
        TYPE_ERROR = 'TYPE_ERROR(%d)'

        sRet = ''
        if self.has_key(id):
            lan_unit = self.lan_dict[id]

            if type == TYPE_FR:
                sRet = lan_unit.get_FR()
            elif type == TYPE_EN:
                sRet = lan_unit.get_EN()
            elif type == TYPE_CH:
                sRet = lan_unit.get_CH()
            else:
                sRet = TYPE_ERROR % type
        else:
            sRet = ID_ERROR  % id

        if sRet == '':
            sRet = 'UNKNOWN ERROR| type->%d | id->%d ', type, id
        return  sRet
 
class ExcelToJson():
    ''''''

    @classmethod
    def open_excel(cls, sPath):
        try:
            cls.data = xlrd.open_workbook(sPath)
        except Exception, e:
            print str(e)

    @classmethod
    def data_to_json(cls, sExcel_Path, sOutput_Path, sheet_index):
        cls.open_excel(sPath)
        if cls.data == None:
            return
        table = cls.data.sheets()[sheet_index]
        nrows = table.nrows
        ncols = table.ncols
        colnames = table.row_values(0)

        jVal = {}
        for i in range(1, nrows):
            id = int(table.cell_value(i, 0))
            ch = table.cell_value(i, 1)
            en = table.cell_value(i, 2)
            fr = table.cell_value(i, 3)

            sub_jVal = {
                'ch':ch,
                'en':en,
                'fr':fr
            }
            jVal[id] = sub_jVal

        # Multilingual
        with open(sOutput_Path, 'w') as json_file:
            json_file.write(json.dumps(jVal, sort_keys=True, indent=4, ensure_ascii=False))#unicode到文字


if __name__ == "__main__":
    etj = ExcelToJson()
    sPath = '123.xlsx'
    sPath1 = 'Multilingual.json'
    etj.data_to_json(sPath, sPath1, 0)
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值