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.