【Python笔记2.2】用zipfile解压zip包时遇到的Unicode字符编解码问题

python unicode字符编解码问题参见【Python笔记2.1】
python中用zipfile解压zip包网上资料一堆,这里就不多说了。
下面使用【Python笔记2.1】中总结出来的字符编解码函数来解决zipfile解压zip包的问题。时间仓促,直接上代码。

完整示例代码(含【Python笔记2.1】中的代码)

# -*- coding: utf-8 -*-
#!/usr/bin/env python2
"""
Created on Mon Jul 23 15:40:00 2018

@author: 
"""
import os
import zipfile


# UTF_8_BOM = b'\xef\xbb\xbf'


def un_zip(filepath, dst_dir):
    try:
        encoding = 'utf-8'
        dst_dir = try_encode(dst_dir, encoding)
        zip_file = zipfile.ZipFile(filepath)
        for names in zip_file.namelist():
            unzip_file = zip_file.extract(names, dst_dir)
            rename_file = os.path.join(dst_dir, try_encode(str_decode(names), encoding))
            os.rename(unzip_file, rename_file)
        zip_file.close()
        return 0, 'unzip Success'
    except Exception as err:
        print('[un_zip]: Exception.ERROR: ', err)
        return -1, 'ERROR, unzip error! please upload the zip package named in utf-8 format.'


def try_encode(s, encoding="utf-8"):
    if s is None:
        print('[tryEncode]: input param None!')
        return s
    try:
        return s.encode(encoding)
    except UnicodeEncodeError as err:
        print(err)


def try_decode(s, decoding="utf-8"):
    try:
        return s.decode(decoding)
    except UnicodeDecodeError as err:
        print(err)


def str_decode(string):
    while True:
        dec = try_decode(string, "utf-8")
        if dec is not None:
            break
        dec = try_decode(string, "ascii")
        if dec is not None:
            break
        dec = try_decode(string, "GB2312")
        if dec is not None:
            break
        dec = try_decode(string, "GBK")
        if dec is not None:
            break
        dec = try_decode(string, "Big5")
        if dec is not None:
            break
        print('[str_decode]: unknown encoding')
        dec = None
        break

    return dec
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值