Beispiel #1
0
    def test_read(self):
        f = NamedTemporaryFile(mode='w+')
        csv_writer = UnicodeWriter(f, lineterminator="\n")
        expected = [['NAME', 'AGE'], ['foo', '12'], ['bar', '16']]
        csv_writer.writerows(expected)
        f.seek(0)

        csv_reader = UnicodeReader(f)
        actual = [line for line in csv_reader]

        self.assertEqual(actual, expected)
Beispiel #2
0
 def test_write(self):
     f = NamedTemporaryFile(mode='w+')
     csv_writer = UnicodeWriter(f, lineterminator="\n")
     csv_writer.writerow(['NAME', 'AGE'])
     csv_writer.writerow(['foo', '12'])
     csv_writer.writerow(['bar', '16'])
     f.seek(0)
     actual = f.read()
     expected = ("NAME,AGE\n"
                 "foo,12\n"
                 "bar,16\n")
     self.assertEqual(actual, expected)
Beispiel #3
0
    def test_read(self):
        f = NamedTemporaryFile(mode='w+')
        csv_writer = UnicodeWriter(f, lineterminator="\n")
        expected = [['NAME', 'AGE'],
                    ['foo', '12'],
                    ['bar', '16']]
        csv_writer.writerows(expected)
        f.seek(0)

        csv_reader = UnicodeReader(f)
        actual = [line for line in csv_reader]

        self.assertEqual(actual, expected)
Beispiel #4
0
 def test_write(self):
     f = NamedTemporaryFile(mode='w+')
     csv_writer = UnicodeWriter(f, lineterminator="\n")
     csv_writer.writerow(['NAME', 'AGE'])
     csv_writer.writerow(['foo', '12'])
     csv_writer.writerow(['bar', '16'])
     f.seek(0)
     actual = f.read()
     expected = ("NAME,AGE\n" "foo,12\n" "bar,16\n")
     self.assertEqual(actual, expected)
    def write(self,
              f,
              delimiter=',',
              quotechar='"',
              quoting=csv.QUOTE_MINIMAL):
        """
        Write the csv to path
        
        @param f: Either a file like object or a file path
        @return UnicodeWriter: The UnicodeWriter instance used to write the csv.
        """
        if not hasattr(f, 'read'):
            handle = open(f, 'wb')
        else:
            handle = f

        writer = UnicodeWriter(handle,
                               delimiter=delimiter,
                               quotechar=quotechar,
                               quoting=quoting)
        writer.writerow(self.report.titles())
        try:
            row_number = 0
            for item in self.qs:
                row = []
                for field in self.report.fields():
                    value = field.prepare_value(field.traverse_for_value(item))
                    if field.pre_process_with:
                        for callback in field.pre_process_with:
                            if hasattr(self.report, callback):
                                value = getattr(self.report, callback)(value,
                                                                       item)
                    rendered = field.widget.render(self.report, self,
                                                   field.name, value, item,
                                                   row_number)
                    if field.post_process_with:
                        for callback in field.post_process_with:
                            try:
                                callback = getattr(self.report, callback)
                            except TypeError:
                                pass
                            rendered = callback(rendered, item)
                    row.append(u"%s" % rendered)
                writer.writerow(row)
                row_number += 1
        except UnicodeDecodeError as e:
            e.reason += ' - for row: %s' % ', '.join(row)
            raise e
        finally:
            if handle != f:
                # If we opened the file, close it.
                handle.close()