def write_to_csv(path, rows, columns, columns_with_hxl=None, remove_group_name=False, dd=None, group_delimiter=DEFAULT_GROUP_DELIMITER, include_labels=False, include_labels_only=False, include_hxl=False, win_excel_utf8=False, total_records=None, index_tags=DEFAULT_INDEX_TAGS, language=None): na_rep = getattr(settings, 'NA_REP', NA_REP) encoding = 'utf-8-sig' if win_excel_utf8 else 'utf-8' with open(path, 'wb') as csvfile: writer = csv.writer(csvfile, encoding=encoding, lineterminator='\n') # Check if to truncate the group name prefix if not include_labels_only: if remove_group_name and dd: new_cols = get_column_names_only(columns, dd, group_delimiter) else: new_cols = columns # use a different group delimiter if needed if group_delimiter != DEFAULT_GROUP_DELIMITER: new_cols = [ group_delimiter.join(col.split(DEFAULT_GROUP_DELIMITER)) for col in new_cols ] writer.writerow(new_cols) if include_labels or include_labels_only: labels = get_labels_from_columns(columns, dd, group_delimiter, language=language) writer.writerow(labels) if include_hxl and columns_with_hxl: hxl_row = [columns_with_hxl.get(col, '') for col in columns] hxl_row and writer.writerow(hxl_row) for i, row in enumerate(rows, start=1): for col in AbstractDataFrameBuilder.IGNORED_COLUMNS: row.pop(col, None) writer.writerow([row.get(col, na_rep) for col in columns]) track_task_progress(i, total_records)
def write_to_csv(path, rows, columns, columns_with_hxl=None, remove_group_name=False, dd=None, group_delimiter=DEFAULT_GROUP_DELIMITER, include_labels=False, include_labels_only=False, include_hxl=False, win_excel_utf8=False, total_records=None, index_tags=DEFAULT_INDEX_TAGS): na_rep = getattr(settings, 'NA_REP', NA_REP) encoding = 'utf-8-sig' if win_excel_utf8 else 'utf-8' with open(path, 'wb') as csvfile: writer = csv.writer(csvfile, encoding=encoding, lineterminator='\n') # Check if to truncate the group name prefix if not include_labels_only: if remove_group_name and dd: new_cols = get_column_names_only(columns, dd, group_delimiter) else: new_cols = columns # use a different group delimiter if needed if group_delimiter != DEFAULT_GROUP_DELIMITER: new_cols = [ group_delimiter.join(col.split(DEFAULT_GROUP_DELIMITER)) for col in new_cols ] writer.writerow(new_cols) if include_labels or include_labels_only: labels = get_labels_from_columns(columns, dd, group_delimiter) writer.writerow(labels) if include_hxl and columns_with_hxl: hxl_row = [columns_with_hxl.get(col, '') for col in columns] hxl_row and writer.writerow(hxl_row) for i, row in enumerate(rows, start=1): for col in AbstractDataFrameBuilder.IGNORED_COLUMNS: row.pop(col, None) writer.writerow([row.get(col, na_rep) for col in columns]) track_task_progress(i, total_records)