How to solve the garbled problem when Python traverses the output name of the zip file,

Source: Internet
Author: User

How to solve the garbled problem when Python traverses the output name of the zip file,

This article describes how to solve the garbled problem when Python traverses the output name of the zip file. Share it with you for your reference. The details are as follows:

In windows, python2.7 is used to traverse the zip file and output the file name and other information. Chinese characters and some punctuation marks printed on the console are garbled. I checked the windows Code mentioned on the Internet as cp936, and the print () function is handed over to the system for printing. Therefore, we need to encode the code in advance to be recognized by windows.

This print Garbled text will also appear in the form of print (mylist) (mylist is a list type variable of python, print (mylist [2]) will not be garbled, strange)

The Code is as follows: (in the. py file, add #-*-coding: UTF-8-*-) in the file header first -*-)

Import zipfiledef listzipfilesinfo (path): z = zipfile. zipFile (path, 'R') try: for filename in z. namelist (): bytes = z. read (filename) print ('file: % s Size: % s' % (unicode (filename, 'cp936 '). decode ('utf-8'), len (bytes) finally: z. close ()

Note: decode can be removed.

I hope this article will help you with Python programming.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.