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 ...
随机推荐
- MFC 关于MFC中CImage的简单使用
首先要将#include <atlimage.h>加进来,开始时我加到stdafx.h中,但一直提示windows.h被重复引入的问题,后将其加在别的头文件中,就可以了.. --! 一 ...
- 两个APP之间怎么调用《IT蓝豹》
两个app之间怎么调用? (1):通过显示Intent 启动 首先:配置好B app 的action,即AndroidManifest.xml中声明 <intent-filter> ...
- C3属性的轮播图(持续更新)
天气好冷,都不想写代码.就先写个没有焦点的轮播图,过两天在补全. 用的是CSS3的属性 过渡transition 和 转换 transfrom:translateX() 只做了轮播和 鼠标进入停止轮播 ...
- Thailand vs Soros
| exchange rate | | Thailand | Soros | |---------------+---------+----------+---------| | | orgin | ...
- CSS text-transform 属性
text-transform 属性控制文本的大小写. h1 {text-transform:uppercase} h2 {text-transform:capitalize} p {text-tran ...
- Linux下SVN客户端安装及使用
转载自:http://www.linuxidc.com/Linux/2015-01/111748.htm 不想自己写了,这个写的挺全的,我就按这个步骤走的,呵呵 非常感谢作者 环境说明: 系统版本:C ...
- java.lang.OutOfMemoryError: Java heap space的解决方法
windows->preferences->Java->Installed JRES->edit->Default VM Arguments添加-Xms256m -Xmx ...
- 【Debian】非法关机后无法联网 connect: network is unreachable
某一天,突然发现无法ssh登录虚拟机内的debian系统,一直认为是ssh的问题,然后无意间ping了ping百度,发现原来是debian系统没有联网....囧 首先,是虚拟机的网络设置检查. 打开V ...
- 持续集成工具Jenkins学习总结
概述 持续集成(Continuous Integration,简称CI)是一种软件开发实践,团队开发人员每次都通过自动化的构建(编译.发布.自动化测试)来验证,从而尽早的发现集成错误.持续集成最大的优 ...
- XE3随笔17:实例 - 模拟 Google 搜索
本例测试效果图: 代码文件: unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics ...