4

I am learning python for the first time and I've just learned that readlines() is incredibly slow and taxing on memory. This would be fine, but as I am programming for a data structures class with up to 10^6 inputs, I believe that runtime is very important.

This is what I have so far that works. I did not strip the '\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 a for loop (which I heard was better), but all I'm getting 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
Saksham Varma
  • 2,122
  • 13
  • 15
nnh12
  • 41
  • 2

3 Answers3

4

Replace stuff with line. stuff is simply the filepath, the actual content is in line -- the variable used for iterating over the generator f

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

You might like to store the list formed after using split on line, as a tuple instead, such that a would be a list of tuples, where each tuple would correspond to a line in the file. You can do that using:

a.append(tuple(line.rstrip('\n').split(",")))
Saksham Varma
  • 2,122
  • 13
  • 15
1

Make sure to name your variables so they make sense. Naming something stuff is convenient but obviously leads to errors. The example below renames this to filename and fixes appending line to the list instead of the filename.

Also, the rstrip function takes a set of characters to strip, so you can strip both \r and \n in one function call. So you would have:

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

This will create a list of lists. If you want to flatten out the inner list in your solution, you will want to use extend instead of append.

Community
  • 1
  • 1
krock
  • 28,904
  • 13
  • 79
  • 85
  • http://stackoverflow.com/questions/29725941/python-read-file-from-command-line-and-strip-n-r-with-very-large-files#comment47586816_29725976 – Ashwini Chaudhary Apr 19 '15 at 06:15
1

I suggest you experiment using the command line interpreter. This makes it easy to learn how rstring and split work. Assuming you start using the line variable as suggested, You probably are not appending to the a list what you want. Also you can strip both \n and \r with one call to 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']]
user873592
  • 226
  • 1
  • 6