def test_write_unicode(self): """ Ensure the function returned from get_csv_row_writer will write a row to the io stream. """ unicode_row_writer = get_csv_row_writer(self.file_stream) for row in self.data: unicode_row_writer(row) self.assertEqual("WĄŻ,2,utf-8 sample\r\ndróżką,utf-8 sample\r\n", self.file_stream.getvalue())
def write_as_csv(items, file_name, append=False, line_buffer_size=None, buffering=FILE_BUFFER_SIZE, get_csv_row_writer=get_csv_row_writer): """ Writes out items to a csv file in groups. :param items: An iterable collection of collections. :param file_name: path to the output file. :param append: whether to append or overwrite the file. :param line_buffer_size: number of lines to write at a time. :param buffering: number of bytes to buffer files :type buffering: int :param get_csv_row_writer: callable that returns a csv row writer function, customize this for non-default options: `custom_writer = partial(get_csv_row_writer, delimiter="|");` `write_as_csv(items, 'my_out_file', get_csv_row_writer=custom_writer)` """ if line_buffer_size is None: line_buffer_size = LINE_BUFFER_SIZE if append: mode = 'a' else: mode = 'w' kwargs = dict(buffering=buffering) if is_py3(): mode += 't' kwargs.update(dict(newline='')) else: mode += 'b' with open(file_name, mode, **kwargs) as csv_file: write_row = get_csv_row_writer(csv_file) batches = i_batch(line_buffer_size, items) for batch in batches: for row in batch: write_row(row)