Python reads the file from the command line and builds "\ n \ r" with very large files

I am learning python for the first time, and I just found out that readlines () is incredibly slow and burdened with memory. That would be nice, but since I am programming a data class with an income of up to 10 ^ 6, I find that runtime is very important.

This is what I still work for. I haven't split '\ r' yet.

def generateListOfPoints(stuff):
    List = open(stuff).readlines()

    a = []

    for i in range(len(List)):
        a.append(List[i].rstrip('\n').split(","))

    return a

This is what I tried to do with the for loop (which I heard was better), but all I get is errors, and I don't know what is going on.

def generateListOfPoints(stuff):

    a = []
    with open(stuff) as f:
        for line in f:
            a.append(stuff.rstrip('\n').rstrip('\r').split(","))
    return a
+4
source share
3 answers

stuff line. stuff - , line - , f

a.append(line.rstrip('\n').split(","))

, , split on line , a , line . , :

a.append(tuple(line.rstrip('\n').split(",")))
+4

, . - stuff , , , . filename line .

, rstrip , \r, \n . , :

def generateListOfPoints(filename):
    a = []
    with open(filename) as f:
        for line in f:
            a.append(line.rstrip('\r\n').split(","))
    return a

. , .

+1

. , rstring split. , , , , , , . \n \r rstrip.

python
>>> a = []
>>> line = "this,is,a,test\n\r"
>>> line.rstrip('\n\r')
'this,is,a,test'
>>> line.rstrip('\n\r').split(',')
['this', 'is', 'a', 'test']
>>> a.append(line.rstrip('\n\r').split(','))
>>> a
[['this', 'is', 'a', 'test']]
+1

All Articles