Ryan Ryan - 7 months ago 28
Python Question

Split every word in line by comma, then store entire line in list

I was wanting to read in a file which has 4 values on each line:
title, author, genre, price.

I want to split each value which has a ',' as the delimiter. Then I want to save it to my List each line being an entry in the list. For example

title, author, genre, price

title2, author2, genre2, price2


This will save as

List[0][1] = title
List[0][2] = author
List[0][3] = genre
List[0][4] = price

List[1][1] = title2
List[1][2] = author2
List[1][3] = genre2
List[1][4] = price2


This is what I have so far:

def readFile(fileName):
List = []
f = open(fileName, 'r')
line = f.readline()
x = 0
while len(line) != 0:
for i in range(4):
List[x][i] = line.split(',')
x += 1
line = f.readline()
f.close()
return List


But I'm just getting
List index out of range
.

Answer

Python has you covered here, just use the csv module:

import csv

def readFile(filename):
    with open(filename, 'rb') as f:
        reader = csv.reader(f)
        return list(reader)

Your code makes several classical errors:

  • str.split() returns a list; you are trying to assign that list 4 times to indices of another list. Just use the list returned by str.split() directly.
  • Take into account that lines from a file come with the line-separator (\n) included; you probably want to strip that off first.
  • You started with an empty list. You can't assign to indices that are not there, use list.append() instead to add elements.
  • You don't need to test for len(line) != 0; just if line: is enough because empty strings are considered 'false' in a truth test. See Truth Value Testing.
  • You don't need to use file.readline() each time; just use a for line in f: loop and you'll get each line one by one, because file objects are iterable.
  • If you use your file as a context manager (by using the with statement), Python will close the file for you.

So, without the csv module, you could write your code like this:

def readFile(fileName):
    rows = []
    with open(fileName, 'r') as f:
        for line in f:
            columns = line.strip().split(',')
            rows.append(columns)
    return rows