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 ...
随机推荐
- python批量处理excel文件数据
https://www.zhihu.com/question/39299070?sort=created 作者:水中柳影链接:https://www.zhihu.com/question/392990 ...
- arguments 对象
在函数体内,标识符arguments是指向实参对象的引用,实参对象是一个类数组对象 arguments[0],arguments.length arguments是什么? 答:1:arguments是 ...
- linux 定时执行php脚本
第一种方法: 1.编写shell脚本: shell文件:/home/www/shell/phpshell.php #!/bin/bash while [ true ]; do /bin/sleep 1 ...
- PowerDesigner反向工程PostgreSQL数据库
1. 环境准备: a) 安装PowerDesigner,以PowerDesigner15.1为例 b) 安装java jdk,以jdk-7-windows-i586为例 ...
- hdu 1849(Rabbit and Grass) 尼姆博弈
Rabbit and Grass Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- java_jdk_JDK版本切换批处理脚本
我们平时在window上做开发的时候,可能需要同时开发两个甚至多个项目,有时不同的项目对JDK的版本要求有区别,这时候我们可能会在一台电脑上安装多个版本的JDK,如下图所示:
- ギリギリ eye (优先队列)
1.题目: [题目描述] A.D.1999,由坠落地球的"谜之战舰"带来的 Over Technology,揭示了人类历史和远 古文明之间的丝丝联系,促使人类终止彼此间的战争,一方 ...
- Java监控工具介绍,VisualVm ,JProfiler,Perfino,Yourkit,Perf4J,JProbe,Java微基准测试
本文是本人前一段时间做一个简单Java监控工具调研总结,主要包括VisualVm ,JProfiler,Perfino,Yourkit,Perf4J,JProbe,以及对Java微基准测试的简单介绍, ...
- hibernate报错Unknown integral data type for ids : java.lang.String
package com.model; // Generated 2016-10-27 14:02:17 by Hibernate Tools 4.3.1.Final /** * CmDept gene ...
- WIN32_LEAN_AND_MEAN宏
网上说: 不加载MFC所需的模块. 用英语解释:Say no to MFC 如果你的工程不使用MFC,就加上这句,这样一来在编译链接时,包括最后生成的一些供调试用的模块时,速度更快,容量更小. 我们经 ...