Stripping whitespace

ryan k ryan at ryankaskel.com
Wed Jan 23 20:17:55 CET 2008


I am taking a database class so I'm not asking for specific answers.
Well I have this text tile:

http://www.cs.tufts.edu/comp/115/projects/proj0/customer.txt

And this code:

# Table and row classes used for queries

class Row(object):
    def __init__(self, column_list, row_vals):
        print len(column_list)
        print len(row_vals)
        for column, value in column_list, row_vals:
            if column and value:
                setattr(self, column.lower(), value)

class Table(object):
    def __init__(self, table_name, table_fd):
        self.name = table_name
        self.table_fd = table_fd
        self.rows = []
        self._load_table()

    def _load_table(self):
        counter = 0
        for line in self.table_fd:
            # Skip the second line
            if not '-----' in line:
                if counter == 0:
                    # This line contains the columns, parse it
                    column_list = line.split()
                else:
                    # This is a row, parse it
                    row_vals = line.split()
                    # Create a new Row object and add it to the
table's
                    # row list
                    self.rows.append(Row(column_list, row_vals))
            counter += 1

Because the addresses contain spaces, this won't work because there
are too many values being unpacked in row's __init__'s for loop. Any
suggestions for a better way to parse this file? I don't want to cheat
but just some general ideas would be nice. Thanks!



More information about the Python-list mailing list