Hertha BSC fan
Hertha BSC fan

Reputation: 77

Put file's content in a list?

I have a text file which I need to store in a list.

FILE:

1 1
4 2
9 10

I would like to have a list of objects. L = [obj obj obj]

CODE:

def putInList(pathToFile):
     myList = []
     with open(pathToFile) as f:
         for line in f:
             s = line.split()
             x, y = [int(v) for v in s]
             jemand = Mensch(x, y)
             myList.append(jemand)
     return myList

This works fine! My problem is that I access DISK MEMORY the number of lines times!

And this sample is artificial I will be working with much bigger files. So I wrote a slicer that puts them into ~100MB .txt files. So I would like to put them in a list without accessing the disk memory million of times.

After search and other questions on StackOverFlow I found this piece of code:

a = open(fileToPath, 'r')
L = [line for line in a.readlines()]

But I have no clue to how to parse a list ?

Upvotes: 2

Views: 75

Answers (2)

Folusho
Folusho

Reputation: 382

readLines() returns a list of the lines in your text file by default. So, what I think you should do is:

def putInList(pathToFile):
    myList = []
    fileHandle = open(pathToFile, "r")
    lines = fileHandle.readLines()
    for line in lines:
        values = line.split()
        x, y = [int(v) for v in values]
        jemand = Mensch(x, y)
        myList.append(jemand)
    return myList

Upvotes: 0

L3viathan
L3viathan

Reputation: 27283

Your assumption is wrong, you will not access disk memory $lines times. Buffering deals with that.

I can make a few recommendations though. Your line splitting logic is more complicated than it needs to be. One way of making it (in my opinion) clearer, is doing:

x, y = map(int, line.split())

That way you don't needlessly create a list that is discarded immediately afterwards.

If you later iterate through myList, and only do it once, you can drop the list entirely, and use a generator function instead:

def putInList(pathToFile):
     with open(pathToFile) as f:
         for line in f:
             x, y = map(int, line.split())
             yield Mensch(x, y)

You can then iterate over it using for mensch in putInList(filename):, although you might want to rename the function in that case. If you still need a list, I would do that regardless and get the list using myList = list(putInList(filename)).

Upvotes: 2

Related Questions