Python3 method of reading UTF-8 file and statistic file line number

Source: Internet
Author: User
In this paper, we describe the method of Python3 reading UTF-8 file and statistic file line number. Share to everyone for your reference. The implementation method is as follows:

"" Created on Dec, Python read the number of rows in the UTF-8 file statistics file @author: Liury_lab "#-*-coding:utf-8-*-import codecs # for smaller File, the simplest way is to read the file into a list of rows, # and then calculate the length of the table by Count = Len (Codecs.open (' d:/freakout.cpp ', ' RU ', ' utf-8 '). ReadLines ()) Print ( Count) # for larger files, can cycle count count =-1 for count, line in enumerate (Codecs.open (' d:/freakout.cpp ', ' RU ', ' Utf-8 ')):  Pass Cou NT + = 1 print (count) # for "\ n" like the Windows end tag, there are also the following options: Count = 0 the_file = codecs.open (' d:/freakout.cpp ', ' RB ', ' Utf-8 ') w Hile (True):   buffer = the_file.read (8192*1024)   if not buffer:     break   

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.