在python中编程导入压缩包,利用zipfile包,从zipinfo读取文件名总是出错,创建的文件名是乱码,写入pgsql更是出错。

但在ubuntu下测试却正常,在windows下测试总是失败。

            if not hdfs.exists_file_dir(dir):
hdfs.make_dir(dir)
hdfs.create_file(hpath, zip.read(info)) # There is no official file name encoding for ZIP files. If you have unicode file names,
# you must convert them to byte strings in your desired encoding before passing them to write().
origin_name = os.path.split(info.filename)[-1].decode(chardet.detect(info.filename)['encoding']) df = DataPart(
id=part_id,
data_title=origin_name,
data_title_en=origin_name,
data_author=data_author,
data_name=hpath,
origin_file_name=origin_name,
remark=remark if remark else 'extracted',
store_type='file',
file_size=info.file_size,
file_ext=file_ext
)
db.session.add(df)
db.session.commit()

初步分析应该是zipfile中的文件名不是unicode编码,将其进行unicode编码应该可以解决。

在zipfile的文档中,有这样一句话:

Note There is no official file name encoding for ZIP files. If you have unicode file names, you must convert them to byte strings in your desired encoding before passing them to write(). WinZip interprets all file names as encoded in CP437, also known as DOS Latin.

  

根据以上,先用chartdet进行文件名编码的判定,然后进行unicode解码,汉字文件名可以正常显示,写入数据库也正常了。

            # There is no official file name encoding for ZIP files. If you have unicode file names,
# you must convert them to byte strings in your desired encoding before passing them to write().
origin_name = os.path.split(info.filename)[-1].decode(chardet.detect(info.filename)['encoding'])

python中zipfile文件名编码的问题的更多相关文章

  1. Python中进行Base64编码和解码

    Base64编码 广泛应用于MIME协议,作为电子邮件的传输编码,生成的编码可逆,后一两位可能有“=”,生成的编码都是ascii字符.优点:速度快,ascii字符,肉眼不可理解缺点:编码比较长,非常容 ...

  2. python中的URL编码和解码

    python中的URL编码和解码:test.py # 引入urllib的request模块 import urllib.request url = 'https://www.douban.com/j/ ...

  3. 如何在Python 中使用UTF-8 编码 && Python 使用 注释,Python ,UTF-8 编码 , Python 注释

    如何在Python 中使用UTF-8 编码 && Python 使用 注释,Python ,UTF-8 编码 , Python  注释 PIP $ pip install beauti ...

  4. Python中zipfile压缩文件模块的使用

    目录 zipfile 压缩一个文件 解压文件 高级应用 利用 zipfile 模块破解压缩文件口令:Python脚本破解压缩文件口令 zipfile Python 中 zipfile 模块提供了对 z ...

  5. python中zipfile模块实例化解析

    文章内容由--“脚本之家“--提供,在此感谢脚本之家的贡献,该网站网址为:https://www.jb51.net/ 简介: zipfile是python里用来做zip格式编码的压缩和解压缩的,由于是 ...

  6. Python学习笔记 (2.2)Python中的字符编码问题及标准数据类型之String(字符串)

    Python3中的String类型 首先,Python中没有字符类型,只有字符串类型.单个字符按照长度为1的字符串处理,这对于曾是OIER的我来说有点不适应啊. 字符串的表示方法 最常用的就是用一对双 ...

  7. python中Url链接编码处理(urlencode,urldecode)

    做完了flask-web应用,这几天想用爬虫做个好玩的电影链接整合器,平时找电影都是在dytt或者dy2018之类的网站,在用dytt搜索电影<美国队长时>,发现他的搜索链接是这样的:ht ...

  8. Python中的Unicode编码和UTF-8编码

    下午看廖雪峰的Python2.7教程,看到 字符串和编码 一节,有一点感受,结合崔庆才的Python博客 ,把这种感受记录下来: ASCII码:是用一个字节(8bit, 0-255)中的127个字母表 ...

  9. python中迷茫的编码问题

    1.理清一些知识点: python默认的编码格式: ASCII(py2) unicode(py3) 查看默认编码:sys.defaultencoding 修改默认编码:#coding = utf-8 ...

随机推荐

  1. CSS动画与GPU

    写在前面 满世界的动画性能优化技巧,例如: 只允许改变transform.opacity,其它属性不要动,避免重新计算布局(reflow) 对动画元素应用transform: translate3d( ...

  2. css中外边距合并

    最近在布局时遇到一个有趣的问题 <style> #div1{width:200px;height:200px;background:red;}  #div2{width:50px;heig ...

  3. Angularjs学习笔记(一)

    大部分传统的模板系统,对模板的渲染是个线性单向的过程:模板或变量与模板混合在一起产生结果的标记集合.任何对模型的改变都需要通过模板的重新计算.但AngularJS有所不同,任何用户引发的视图的改变,都 ...

  4. photoshop cs5 key

    序列号: 1330-1164-2870-9234-4243-7879 1330-1027-8489-4513-0233-4890 1330-1176-2865-0373-1551-0175 1330- ...

  5. Command(命令)-对象行为型模式

    1.意图 将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化:对请求排队或记录请求日志,以及支持可撤销的操作. 2.别名 动作(Action),事务(Transaction) 3.动机 ...

  6. chVsprintf

    #if LINUX_SYSTEM int chVsprintf(LPSTR buffer, int nCount, LPCSTR format, va_list argptr) { return vs ...

  7. 初涉定制linux系统之——自动化安装Centos系统镜像制作

    最近碰到个需求:要在内网环境安装centos6.5系统并搭建服务,但由于自动部署脚本里安装依赖包使用的是yum安装,而服务器无法连接外网,实施人员也不会本地yum源搭建O__O "….. 本 ...

  8. Android资源文件简介

    Android资源文件简介 1. Android应用资源的作用 (1) Android项目中文件分类 在Android工程中, 文件主要分为下面几类 : 界面布局文件, Java src源文件, 资源 ...

  9. UItextFiled只能输入数字

    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementS ...

  10. Spark standalone HA

    配置Spark standalone HA 主机:node1,node2,node3 master: node1,node2 slave:node2,node3 修改配置文件: node1,node3 ...