Workaround for Python to traverse the zip file output name when the garbled problem occurs

Source: Internet
Author: User
Tags python list
This article describes a workaround for Python's garbled behavior when traversing the output name of a zip file. Share to everyone for your reference. Specific as follows:

Windows uses python2.7 to traverse the zip file after the output file name and other information, console printing Chinese and some punctuation is garbled. I checked the Internet. The encoding for Windows is Cp936,print () function to the system to process printing, so it is encoded in advance to the encoding that Windows can recognize.

This print garbled also appears in the form of print (MyList) (MyList is a python list type variable, print (Mylist[2]) is not garbled, strange)

The code is as follows: (add #-*-coding:utf-8–*-to the file header in the. py file)

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

Hopefully 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.