Changeset 6114
- Timestamp:
- Jan 5, 2009, 9:24:07 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
anuga_core/source/anuga/shallow_water/data_manager.py
r6086 r6114 1031 1031 def csv2dict(file_name, title_check_list=None): 1032 1032 """ 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, 1035 1035 to keep track of the column order. 1036 1036 … … 1042 1042 """ 1043 1043 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 1044 1048 attribute_dic = {} 1045 1049 title_index_dic = {} 1046 titles_stripped = [] # list of titles1050 titles_stripped = [] # List of titles 1047 1051 1048 1052 reader = csv.reader(file(file_name)) … … 1050 1054 # Read in and manipulate the title info 1051 1055 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 1055 1060 title_count = len(titles_stripped) 1056 1061 1057 # check required columns1062 # Check required columns 1058 1063 if title_check_list is not None: 1059 1064 for title_check in title_check_list: 1060 #msg = "Reading error. This row is not present ", title_check1061 #assert title_index_dic.has_key(title_check), msg1062 1065 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 1065 1067 raise IOError, msg 1066 1068 1067 # create a dicof column values, indexed by column title1069 # Create a dictionary of column values, indexed by column title 1068 1070 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 1071 1076 for i, value in enumerate(line): 1072 1077 attribute_dic.setdefault(titles_stripped[i], []).append(value)
Note: See TracChangeset
for help on using the changeset viewer.