Changeset 6114


Ignore:
Timestamp:
Jan 5, 2009, 9:24:07 PM (15 years ago)
Author:
ole
Message:

Cosmetics in csv2dict

File:
1 edited

Legend:

Unmodified
Added
Removed
  • anuga_core/source/anuga/shallow_water/data_manager.py

    r6086 r6114  
    10311031def csv2dict(file_name, title_check_list=None):
    10321032    """
    1033     Load in the csv as a dic, title as key and column info as value.
    1034     Also, create a dic, title as key and column index as value,
     1033    Load in the csv as a dictionary, title as key and column info as value.
     1034    Also, create a dictionary, title as key and column index as value,
    10351035    to keep track of the column order.
    10361036
     
    10421042    """
    10431043
     1044    # FIXME(Ole): Consider dealing with files without headers
     1045    # FIXME(Ole): Consider a wrapper automatically converting text fields
     1046    #             to the right type by trying for: int, float, string
     1047   
    10441048    attribute_dic = {}
    10451049    title_index_dic = {}
    1046     titles_stripped = [] # list of titles
     1050    titles_stripped = [] # List of titles
    10471051
    10481052    reader = csv.reader(file(file_name))
     
    10501054    # Read in and manipulate the title info
    10511055    titles = reader.next()
    1052     for i,title in enumerate(titles):
    1053         titles_stripped.append(title.strip())
    1054         title_index_dic[title.strip()] = i
     1056    for i, title in enumerate(titles):
     1057        header = title.strip()
     1058        titles_stripped.append(header)
     1059        title_index_dic[header] = i
    10551060    title_count = len(titles_stripped)
    10561061
    1057     # check required columns
     1062    # Check required columns
    10581063    if title_check_list is not None:
    10591064        for title_check in title_check_list:
    1060             #msg = "Reading error. This row is not present ", title_check
    1061             #assert title_index_dic.has_key(title_check), msg
    10621065            if not title_index_dic.has_key(title_check):
    1063                 #reader.close()
    1064                 msg = "Reading error. This row is not present ", title_check
     1066                msg = 'Reading error. This row is not present %s' % title_check
    10651067                raise IOError, msg
    10661068
    1067     #create a dic of column values, indexed by column title
     1069    # Create a dictionary of column values, indexed by column title
    10681070    for line in reader:
    1069         if len(line) <> title_count:
    1070             raise IOError #FIXME make this nicer
     1071        n = len(line) # Number of entries
     1072        if n != title_count:
     1073            msg = 'Entry in file %s had %d columns ' % (file_name, n)
     1074            msg += 'although there were %d headers' % title_count
     1075            raise IOError, msg
    10711076        for i, value in enumerate(line):
    10721077            attribute_dic.setdefault(titles_stripped[i], []).append(value)
Note: See TracChangeset for help on using the changeset viewer.