Splitting a list of strings

Mark McEahern marklists at mceahern.com
Tue Sep 17 23:31:36 EDT 2002


> Okay. Here is some representative sample data:

This does everything except ignore the last attribute--easy enough to add
either here or have the caller do it.

def getDataAndAttributes2(filename):
    attrib_prefix = "@"
    comment_prefix = "%"
    attributes = []
    data = []
    f = file(filename)
    lines = f.readlines()
    f.close()
    for line in lines:
        # Ignore empty lines and comments.
        line = line.strip()
        if not line or line.startswith(comment_prefix):
            continue
        # Put attributes and data into separate lists.
        if line.startswith(attrib_prefix):
            attributes.append(line)
        else:
            data.append(line)
    return data, attributes

// m

-





More information about the Python-list mailing list