The fastest way to read input in Python

后端 未结 6 1578
孤街浪徒
孤街浪徒 2020-11-27 07:56

I want to read a huge text file that contains list of lists of integers. Now I\'m doing the following:

G = []
with open(\"test.txt\", \'r\') as f:
    for li         


        
6条回答
  •  不知归路
    2020-11-27 08:16

    As a general rule of thumb (for just about any language), using read() to read in the entire file is going to be quicker than reading one line at a time. If you're not constrained by memory, read the whole file at once and then split the data on newlines, then iterate over the list of lines.

提交回复
热议问题