python中zipfile文件名编码的问题
在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文件名编码的问题的更多相关文章
- Python中进行Base64编码和解码
Base64编码 广泛应用于MIME协议,作为电子邮件的传输编码,生成的编码可逆,后一两位可能有“=”,生成的编码都是ascii字符.优点:速度快,ascii字符,肉眼不可理解缺点:编码比较长,非常容 ...
- python中的URL编码和解码
python中的URL编码和解码:test.py # 引入urllib的request模块 import urllib.request url = 'https://www.douban.com/j/ ...
- 如何在Python 中使用UTF-8 编码 && Python 使用 注释,Python ,UTF-8 编码 , Python 注释
如何在Python 中使用UTF-8 编码 && Python 使用 注释,Python ,UTF-8 编码 , Python 注释 PIP $ pip install beauti ...
- Python中zipfile压缩文件模块的使用
目录 zipfile 压缩一个文件 解压文件 高级应用 利用 zipfile 模块破解压缩文件口令:Python脚本破解压缩文件口令 zipfile Python 中 zipfile 模块提供了对 z ...
- python中zipfile模块实例化解析
文章内容由--“脚本之家“--提供,在此感谢脚本之家的贡献,该网站网址为:https://www.jb51.net/ 简介: zipfile是python里用来做zip格式编码的压缩和解压缩的,由于是 ...
- Python学习笔记 (2.2)Python中的字符编码问题及标准数据类型之String(字符串)
Python3中的String类型 首先,Python中没有字符类型,只有字符串类型.单个字符按照长度为1的字符串处理,这对于曾是OIER的我来说有点不适应啊. 字符串的表示方法 最常用的就是用一对双 ...
- python中Url链接编码处理(urlencode,urldecode)
做完了flask-web应用,这几天想用爬虫做个好玩的电影链接整合器,平时找电影都是在dytt或者dy2018之类的网站,在用dytt搜索电影<美国队长时>,发现他的搜索链接是这样的:ht ...
- Python中的Unicode编码和UTF-8编码
下午看廖雪峰的Python2.7教程,看到 字符串和编码 一节,有一点感受,结合崔庆才的Python博客 ,把这种感受记录下来: ASCII码:是用一个字节(8bit, 0-255)中的127个字母表 ...
- python中迷茫的编码问题
1.理清一些知识点: python默认的编码格式: ASCII(py2) unicode(py3) 查看默认编码:sys.defaultencoding 修改默认编码:#coding = utf-8 ...
随机推荐
- sqlserver 查看正在执行sql
SELECT [session_id], [request_id], [cpu_time], [start_time] AS '开始时间', [status] AS '状态', [co ...
- the server quit without updating pid file (/var/lib/mysql/localhost.localdomain.pid)
前几天装的mysql,用的还挺爽的,第二天再用就不行了,报的错误如标题.网上也是众说纷纭,可能有很多原因会导致这种错误吧.我用的是将Mysqld这个进程杀掉,就可以启动mysql了
- 阿里云SLB后Nginx、Tomcat获取真实IP
一.SLB后Nginx如何获取真实IP 前提:nginx作为slb获取真实ip是使用 http_realip_module,默认一键安装包安装的nginx没有安装这个模块需要重新重新编译nginx并加 ...
- c++截取英文和汉字(单双字节)混合字符串
在C++里截取字符串可以使用CString.Mid(),可是这个函数只能按英文(单字节)来截取, 如果是汉字可能就要计算好字符个数,如果是汉字和英文混合,那就没辙了. 可是恰好我需要这样一个函数,于是 ...
- What is “:-!!” in C code?
stackoverflow上看到的这个问题,觉得挺有趣,顺手记下来. 楼主提问: I bumped into this strange macro code in /usr/include/linux ...
- 关于apache httpd.conf脚本的理解
新人一枚,这两天一直在研究lamp的搭建,感觉自己对apache理解的不够深彻,决定写这一篇(翻译)httpd.conf文件 未完待续 cat /usr/local/apache/conf/httpd ...
- java 零碎知识点
1. 字符串有整型的相互转换 1 2 String a = String.valueOf(2); //integer to numeric string int i = Integer.pars ...
- 显示天气预报的Demo
实现的效果如下所示: 代码如下: ViewController.h #import <UIKit/UIKit.h> @interface ViewController : UIViewCo ...
- Citrix Reciver提示安装程序集错误
打开注册表查找是否有RegistrySizeLimit这个项目,如果有更改成8个f,如果没有就需要手动建立一个之后输入8个f,操作方法如下: 到了 HKEY_LOCAL_MACHINE\SYSTEM\ ...
- 05 Apache Solr: 管理员界面(Admin UI)
为了方便管理员和工程师调整Solr的配置和访问在线文档和其他的帮助,Solr提供了一个Web界面去查看Solr的配置详情,运行查询语句和分析文档字段.这个界面在第三篇里面提到过就是管理员界面 ...