Index: trunk/tools/editor_trends/utils/utils.py |
— | — | @@ -215,20 +215,19 @@ |
216 | 216 | fh.write('\n') |
217 | 217 | |
218 | 218 | |
219 | | -def write_dict_to_csv(data, fh, write_key=True, newline=True): |
220 | | - keys = data.keys() |
221 | | - keys.sort() |
| 219 | +def write_dict_to_csv(data, fh, keys, write_key=True, newline=True): |
222 | 220 | for key in keys: |
223 | 221 | if write_key: |
224 | 222 | fh.write('%s\t' % key) |
225 | | - if getattr(data[key], '__iter__', False): |
| 223 | + if type(data[key]) == type(list): |
| 224 | + write_list_to_csv(data[key], fh, recursive=False, newline=False) |
| 225 | + elif getattr(data[key], '__iter__', False): |
226 | 226 | for d in data[key]: |
227 | 227 | fh.write('%s\t' % d) |
228 | 228 | else: |
229 | 229 | fh.write('%s\t' % (data[key])) |
230 | 230 | if newline: |
231 | 231 | fh.write('\n') |
232 | | - return False #this prevents the calling function from writing \t |
233 | 232 | |
234 | 233 | |
235 | 234 | def create_txt_filehandle(location, name, mode, encoding): |
— | — | @@ -315,15 +314,26 @@ |
316 | 315 | return dict([[v, k] for k, v in dictionary.items()]) |
317 | 316 | |
318 | 317 | |
319 | | -def create_dict_from_csv_file(location, filename, encoding): |
| 318 | +def create_dict_from_csv_file(location, filename, encoding, keys=None): |
320 | 319 | ''' |
321 | 320 | Constructs a dictionary from a txtfile |
| 321 | + The first column of the csv file should contain the main key for the dictionary. |
| 322 | + If there are more than one value in the values list, then a @keys variable should |
| 323 | + be supplied and the key sequence should match the value sequence. |
322 | 324 | ''' |
323 | 325 | d = {} |
324 | 326 | for line in read_data_from_csv(location, filename, encoding): |
325 | 327 | line = clean_string(line) |
326 | | - value, key = line.split('\t') |
327 | | - d[key] = value |
| 328 | + line = line.split('\t') |
| 329 | + key = line[0] |
| 330 | + values = line[1:] |
| 331 | + if len(values) == 1: |
| 332 | + d[key] = values |
| 333 | + else: |
| 334 | + assert keys != None |
| 335 | + d[key] = {} |
| 336 | + for k, v in zip(keys, values): |
| 337 | + d[key][k] = v |
328 | 338 | return d |
329 | 339 | |
330 | 340 | |