def test_can_write_delimited_data_to_string_io(self): delimited_data_format = data.DataFormat(data.FORMAT_DELIMITED) delimited_data_format.validate() with io.StringIO() as target: with rowio.DelimitedRowWriter(target, delimited_data_format) as delimited_writer: delimited_writer.write_row(['a', 'b', _EURO_SIGN]) delimited_writer.write_row([]) delimited_writer.write_row([1, 2, 'end']) data_written = dev_test.unified_newlines(target.getvalue()) self.assertEqual('%r' % data_written, '%r' % 'a,b,\u20ac\n\n1,2,end\n')
def test_can_write_delimited_data_to_path(self): delimited_data_format = data.DataFormat(data.FORMAT_DELIMITED) delimited_data_format.set_property(data.KEY_ENCODING, 'utf-8') delimited_data_format.validate() delimited_path = dev_test.path_to_test_result('test_can_write_delimited_to_path.csv') with io.open(delimited_path, 'w', newline='', encoding=delimited_data_format.encoding) as delimited_target_stream: with rowio.DelimitedRowWriter(delimited_target_stream, delimited_data_format) as delimited_writer: delimited_writer.write_row(['a', 'b', _EURO_SIGN]) delimited_writer.write_row([]) delimited_writer.write_row([1, 2, 'end']) with io.open(delimited_path, 'r', encoding=delimited_data_format.encoding) as delimited_source_stream: # Note: all kinds of newline characters are translated to '\n' because of newline=None. data_written = delimited_source_stream.read() self.assertEqual('%r' % data_written, '%r' % 'a,b,\u20ac\n\n1,2,end\n')
def test_fails_on_unicode_error_during_delimited_write(self): delimited_data_format = data.DataFormat(data.FORMAT_DELIMITED) delimited_data_format.set_property(data.KEY_ENCODING, 'ascii') delimited_data_format.validate() delimited_path = dev_test.path_to_test_result('test_fails_on_unicode_error_during_delimited_write.csv') with io.open(delimited_path, 'w', newline='', encoding=delimited_data_format.encoding) as delimited_target_stream: with rowio.DelimitedRowWriter(delimited_target_stream, delimited_data_format) as delimited_writer: try: delimited_writer.write_row(['a']) delimited_writer.write_row(['b', _EURO_SIGN]) self.fail() except errors.DataError as anticipated_error: anticipated_error_message = str(anticipated_error) dev_test.assert_fnmatches( self, anticipated_error_message, "*.csv (R2C1): cannot write data row: *; row=*'b', *")
def __init__(self, cid_or_path, target): assert cid_or_path is not None assert target is not None super(Writer, self).__init__(cid_or_path) data_format = cid_or_path.data_format assert self.cid.data_format.is_valid self._header = data_format.header self._delegated_writer = None if data_format.format == data.FORMAT_DELIMITED: self._delegated_writer = rowio.DelimitedRowWriter(target, data_format) elif data_format.format == data.FORMAT_FIXED: self._field_names_and_lengths = interface.field_names_and_lengths(self.cid) self._delegated_writer = rowio.FixedRowWriter(target, data_format, self._field_names_and_lengths) else: raise NotImplementedError('data_format=%r' % data_format.format)