Lazy Method for Reading Big File in Python?

前端 未结 12 1752
谎友^
谎友^ 2020-11-21 06:40

I have a very big file 4GB and when I try to read it my computer hangs. So I want to read it piece by piece and after processing each piece store the processed piece into an

12条回答
  •  太阳男子
    2020-11-21 07:00

    To process line by line, this is an elegant solution:

      def stream_lines(file_name):
        file = open(file_name)
        while True:
          line = file.readline()
          if not line:
            file.close()
            break
          yield line
    

    As long as there're no blank lines.

提交回复
热议问题