1fy07h 发表于 2024-10-7 16:37:33

Python代码解压的中文文件名是乱码,怎么处理?


    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">大众</span>好,我是Python进阶者。</span></p>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">1、</span>前言</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">前几天在Python白银交流群【fashjon】问了一个Python自动化办公的问题,问题如下:</span></p><span style="color: black;">import</span> zipfile

    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">unzip_file</span><span style="color: black;">(zip_file_path, output_folder_path)</span>:</span>
    <span style="color: black;">with</span>zipfile.ZipFile(zip_file_path,<span style="color: black;">r</span>) <span style="color: black;">as</span> zip_ref:
    zip_ref.extractall(output_folder_path)

    <span style="color: black;"># <span style="color: black;">运用</span>示例</span>
    zip_file_path = <span style="color: black;">example.zip</span>
    output_folder_path = <span style="color: black;">output_folder</span>unzip_file(zip_file_path, output_folder_path)<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">以上代码解压的中文文件名是乱码,怎么<span style="color: black;">处理</span>?</span></p>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">2、</span>实现过程</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">这儿</span>【瑜亮老师】给了个思路如下:乱码是什么样子的?最好能传上来一个压缩包,<span style="color: black;">咱们</span>好<span style="color: black;">实验</span>。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">过了几分钟,【瑜亮老师】<span style="color: black;">弥补</span>道:@fashjon 我查看了一下zipfile的源码,<span style="color: black;">发掘</span>是<span style="color: black;">运用</span>了cp437编码了文件名才<span style="color: black;">引起</span>的文件名错误。既然<span style="color: black;">晓得</span>了错误的编码,<span style="color: black;">那样</span><span style="color: black;">办法</span>就很简单了,1修改源码,2自己写代码读取解压缩路径中的文件名,重新<span style="color: black;">运用</span>cp437编码<span style="color: black;">而后</span>用gbk解码,重命名文件名,问题<span style="color: black;">处理</span>。修改后的代码如下:</span></p><span style="color: black;">import</span> zipfile
    <span style="color: black;">import</span> os
    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">unzip_file</span><span style="color: black;">(zip_file_path, output_folder_path, encoding=<span style="color: black;">gbk</span>)</span>:</span>
    <span style="color: black;">with</span> zipfile.ZipFile(zip_file_path, <span style="color: black;">r</span>) <span style="color: black;">as</span> zip_ref:
    zip_ref.extractall(output_folder_path)
    <span style="color: black;"># 遍历解压后的目录,<span style="color: black;">检测</span>并修正文件名</span>
    <span style="color: black;">for</span> root, dirs, files <span style="color: black;">in</span> os.walk(output_folder_path):
    <span style="color: black;">for</span> bad_name <span style="color: black;">in</span> files:
    <span style="color: black;"># 假设<span style="color: black;">这儿</span><span style="color: black;">咱们</span><span style="color: black;">晓得</span>ZIP文件<span style="color: black;">运用</span>的是gbk编码(但<span style="color: black;">实质</span><span style="color: black;">状况</span>可能<span style="color: black;">区别</span>)</span>
    <span style="color: black;">if</span> <span style="color: black;">not</span> bad_name.isascii(): <span style="color: black;"># <span style="color: black;">倘若</span>文件名<span style="color: black;">包括</span>非ASCII字符</span>true_name = bad_name.encode(<span style="color: black;">cp437</span>).decode(encoding)
    os.rename(os.path.join(root, bad_name), os.path.join(root, true_name))
    <span style="color: black;"># <span style="color: black;">运用</span>示例</span>
    zip_file_path = <span style="color: black;">example.zip</span>
    output_folder_path = <span style="color: black;">output_folder</span>
    unzip_file(zip_file_path, output_folder_path)
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">不确定你的电脑里<span style="color: black;">是不是</span>安装pathlib,<span style="color: black;">因此呢</span><span style="color: black;">运用</span>了os,顺利地<span style="color: black;">处理</span>了粉丝的疑问。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">粉丝追问:解压文件用什么库比较好?</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">回答1:这方面我只<span style="color: black;">晓得</span>zipfile和代码调用winrar。平时很少接触匹量处理压缩包。</span></p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-axegupay5k/25de6d5287bd49bea4cbf1f53d41f620~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1728386341&amp;x-signature=DWujes8ILS5tkva9KsucbWvzYKE%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">回答2:批量处理压缩包,直接wps批量解压就<span style="color: black;">能够</span>了;</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">回答3:何须wps,只要电脑上装的有任意解压软件,全选压缩包,右键,<span style="color: black;">能够</span>解压到一个文件夹,<span style="color: black;">亦</span><span style="color: black;">能够</span><span style="color: black;">每一个</span>压缩包单独生成一个文件夹。代码只<span style="color: black;">不外</span>方便遍历多层文件夹目录,<span style="color: black;">倘若</span>都在一个文件夹中,还不如右键点选更方便。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">倘若</span>你<span style="color: black;">亦</span>有类似这种Python<span style="color: black;">关联</span>的小问题,欢迎随时来交流群学习交流哦,有问必答!</span></p>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">3、</span>总结</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">大众</span>好,我是Python进阶者。这篇<span style="color: black;">文案</span><span style="color: black;">重点</span>盘点了一个Python自动化办公的问题,文中针对该问题,给出了<span style="color: black;">详细</span>的解析和代码实现,<span style="color: black;">帮忙</span>粉丝顺利<span style="color: black;">处理</span>了问题。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">最后感谢粉丝【fashjon】提出的问题,感谢【瑜亮老师】给出的思路,感谢【莫生气】等人参与学习交流。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">【提问<span style="color: black;">弥补</span>】温馨提示,<span style="color: black;">大众</span>在群里提问的时候。<span style="color: black;">能够</span><span style="color: black;">重视</span>下面几点:<span style="color: black;">倘若</span><span style="color: black;">触及</span>到大文件数据,<span style="color: black;">能够</span>数据脱敏后,发点demo数据来(小文件的意思),<span style="color: black;">而后</span>贴点代码(<span style="color: black;">能够</span>复制的那种),记得发报错截图(截全)。代码不多的话,直接发代码文字<span style="color: black;">就可</span>,代码超过50行这样的话,发个.py文件就行。</span></p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-6w9my0ksvp/724e89bc876f421384929eaad57ab312~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1728386341&amp;x-signature=M4qH0poOskofKd4AkCx%2B%2FUyu3ZY%3D" style="width: 50%; margin-bottom: 20px;"></div>




qzmjef 发表于 2024-10-27 01:51:28

你的见解真是独到,让我受益匪浅。

4lqedz 发表于 2024-11-1 22:18:14

你的见解独到,让我受益匪浅,非常感谢。

1fy07h 发表于 2024-11-10 00:20:27

谷歌网站排名优化 http://www.fok120.com/
页: [1]
查看完整版本: Python代码解压的中文文件名是乱码,怎么处理?