Beispiel #1
0
    def test_csv(self):
        filepath = p.join(io.DATA_DIR, 'test.csv')
        header = ['some_date', 'sparse_data', 'some_value', 'unicode_test']

        with open(filepath, 'r', encoding='utf-8') as f:
            records = io._read_csv(f, header)
            nt.assert_equal(self.sheet0_alt, next(records))

        filepath = p.join(io.DATA_DIR, 'no_header_row.csv')
        records = io.read_csv(filepath, has_header=False)
        expected = {'column_1': '1', 'column_2': '2', 'column_3': '3'}
        nt.assert_equal(expected, next(records))

        filepath = p.join(io.DATA_DIR, 'test_bad.csv')
        kwargs = {'sanitize': True, 'first_row': 1, 'first_col': 1}
        records = io.read_csv(filepath, **kwargs)
        nt.assert_equal(self.sheet0_alt, next(records))

        filepath = p.join(io.DATA_DIR, 'fixed_w_header.txt')
        widths = [0, 18, 29, 33, 38, 50]
        records = io.read_fixed_fmt(filepath, widths, has_header=True)
        expected = {
            'News Paper': 'Chicago Reader',
            'Founded': '1971-01-01',
            'Int': '40',
            'Bool': 'True',
            'Float': '1.0',
            'Timestamp': '04:14:001971-01-01T04:14:00'
        }

        nt.assert_equal(expected, next(records))
Beispiel #2
0
    def test_csv(self):
        filepath = p.join(io.DATA_DIR, 'test.csv')
        header = ['some_date', 'sparse_data', 'some_value', 'unicode_test']

        with open(filepath, 'rU', encoding='utf-8') as f:
            records = io._read_csv(f, header)
            nt.assert_equal(self.sheet0_alt, next(records))

        filepath = p.join(io.DATA_DIR, 'no_header_row.csv')
        records = io.read_csv(filepath, has_header=False)
        expected = {'column_1': '1', 'column_2': '2', 'column_3': '3'}
        nt.assert_equal(expected, next(records))

        filepath = p.join(io.DATA_DIR, 'test_bad.csv')
        kwargs = {'sanitize': True, 'first_row': 1, 'first_col': 1}
        records = io.read_csv(filepath, **kwargs)
        nt.assert_equal(self.sheet0_alt, next(records))

        filepath = p.join(io.DATA_DIR, 'fixed_w_header.txt')
        widths = [0, 18, 29, 33, 38, 50]
        records = io.read_fixed_fmt(filepath, widths, has_header=True)
        expected = {
            'News Paper': 'Chicago Reader',
            'Founded': '1971-01-01',
            'Int': '40',
            'Bool': 'True',
            'Float': '1.0',
            'Timestamp': '04:14:001971-01-01T04:14:00'}

        nt.assert_equal(expected, next(records))
Beispiel #3
0
    def test_opened_files(self):
        """Test for reading open files"""
        filepath = p.join(io.DATA_DIR, 'test.csv')
        header = ['some_date', 'sparse_data', 'some_value', 'unicode_test']

        with open(filepath, encoding='utf-8') as f:
            records = io._read_csv(f, header)  # pylint: disable=W0212
            nt.assert_equal(self.sheet0_alt, next(records))

        f = open(filepath, encoding='utf-8')

        try:
            records = io.read_csv(f, sanitize=True)
            nt.assert_equal(self.sheet0_alt, next(records))
        finally:
            f.close()

        f = open(filepath, 'rU', newline=None)

        try:
            records = io.read_csv(f, sanitize=True)
            nt.assert_equal(self.sheet0_alt, next(records))
        finally:
            f.close()

        filepath = p.join(io.DATA_DIR, 'test.xlsx')

        with open(filepath, 'r+b') as f:
            records = io.read_xls(f, sanitize=True, sheet=0)
            nt.assert_equal(self.sheet0, next(records))

        f = open(filepath, 'r+b')

        try:
            records = io.read_xls(f, sanitize=True, sheet=0)
            nt.assert_equal(self.sheet0, next(records))
        finally:
            f.close()
Beispiel #4
0
    def test_opened_files(self):
        """Test for reading open files"""
        filepath = p.join(io.DATA_DIR, 'test.csv')
        header = ['some_date', 'sparse_data', 'some_value', 'unicode_test']

        with open(filepath, encoding='utf-8') as f:
            records = io._read_csv(f, header)  # pylint: disable=W0212
            nt.assert_equal(self.sheet0_alt, next(records))

        f = open(filepath, encoding='utf-8')

        try:
            records = io.read_csv(f, sanitize=True)
            nt.assert_equal(self.sheet0_alt, next(records))
        finally:
            f.close()

        f = open(filepath, 'rU', newline=None)

        try:
            records = io.read_csv(f, sanitize=True)
            nt.assert_equal(self.sheet0_alt, next(records))
        finally:
            f.close()

        filepath = p.join(io.DATA_DIR, 'test.xlsx')

        with open(filepath, 'r+b') as f:
            records = io.read_xls(f, sanitize=True, sheet=0)
            nt.assert_equal(self.sheet0, next(records))

        f = open(filepath, 'r+b')

        try:
            records = io.read_xls(f, sanitize=True, sheet=0)
            nt.assert_equal(self.sheet0, next(records))
        finally:
            f.close()