请问python3.6版本读取zip压缩包里中文文件乱码的问题

江南大富翁 2017-09-08 09:03:05
直接读取压缩包里中文文件名显示都是乱码,网上搜索了这个解决方法
fileZip=zipfile.ZipFile(filename)
for file_info in fileZip.infolist():
filename=unicode(file_info.filename,'GBK').encode('utf8')

报错这个
Traceback (most recent call last):
File "E:\2.py", line 6, in <module>
filename=unicode(file_info.filename,'GBK').encode('utf8')
NameError: name 'unicode' is not defined
>>>

网上有说py3版本str代替Unicode了,可是使用str也报错,请教各位高手该如何解决?
...全文
353 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
混沌鳄鱼 2017-09-08
  • 打赏
  • 举报
回复
filename=file_info.filename.decode('gbk').encode('utf8') 改成 filename=file_info.encode('utf8')
江南大富翁 2017-09-08
  • 打赏
  • 举报
回复
引用 楼主 gavin851027 的回复:
直接读取压缩包里中文文件名显示都是乱码,网上搜索了这个解决方法 fileZip=zipfile.ZipFile(filename) for file_info in fileZip.infolist(): filename=unicode(file_info.filename,'GBK').encode('utf8') 报错这个 Traceback (most recent call last): File "E:\2.py", line 6, in <module> filename=unicode(file_info.filename,'GBK').encode('utf8') NameError: name 'unicode' is not defined >>> 网上有说py3版本str代替Unicode了,可是使用str也报错,请教各位高手该如何解决?
修改之后会报错这个 filename=file_info.filename.decode('gbk').encode('utf8') AttributeError: 'str' object has no attribute 'decode'
oyljerry 2017-09-08
  • 打赏
  • 举报
回复
file_info.filename 已经是Unicode了,直接decode
混沌鳄鱼 2017-09-08
  • 打赏
  • 举报
回复
把 filename=unicode(file_info.filename,'GBK').encode('utf8') 改成 filename=file_info.filename.decode('gbk').encode('utf8')

37,718

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧