The solution to the garbled problem when Python traverses the zip file output name _python

Source: Internet
Author: User
Tags python list

The example in this article describes the solution to the garbled problem when Python traverses the output name of the zip file. Share to everyone for your reference. Specifically as follows:

Windows in the use of python2.7 traverse the zip file after the output file name information, console printing Chinese and some punctuation appears garbled. Check it out. The Windows encoding for the Cp936,print () function is given to the system to process the printing, so encode it in advance to the code that Windows recognizes.

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: (in the. py file, first add #-*-coding:utf-8–*-in the file header)

Import ZipFile
def 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 your Python programming.

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.