|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +""" |
| 4 | +Tests that quoting specifications are properly handled |
| 5 | +during parsing for all of the parsers defined in parsers.py |
| 6 | +""" |
| 7 | + |
| 8 | +import csv |
| 9 | +import pandas.util.testing as tm |
| 10 | + |
| 11 | +from pandas import DataFrame |
| 12 | +from pandas.compat import StringIO |
| 13 | + |
| 14 | + |
| 15 | +class QuotingTests(object): |
| 16 | + |
| 17 | + def test_bad_quote_char(self): |
| 18 | + data = '1,2,3' |
| 19 | + |
| 20 | + # Python 2.x: "...must be an 1-character..." |
| 21 | + # Python 3.x: "...must be a 1-character..." |
| 22 | + msg = '"quotechar" must be a(n)? 1-character string' |
| 23 | + tm.assertRaisesRegexp(TypeError, msg, self.read_csv, |
| 24 | + StringIO(data), quotechar='foo') |
| 25 | + |
| 26 | + msg = 'quotechar must be set if quoting enabled' |
| 27 | + tm.assertRaisesRegexp(TypeError, msg, self.read_csv, |
| 28 | + StringIO(data), quotechar=None, |
| 29 | + quoting=csv.QUOTE_MINIMAL) |
| 30 | + |
| 31 | + msg = '"quotechar" must be string, not int' |
| 32 | + tm.assertRaisesRegexp(TypeError, msg, self.read_csv, |
| 33 | + StringIO(data), quotechar=2) |
| 34 | + |
| 35 | + def test_bad_quoting(self): |
| 36 | + data = '1,2,3' |
| 37 | + |
| 38 | + msg = '"quoting" must be an integer' |
| 39 | + tm.assertRaisesRegexp(TypeError, msg, self.read_csv, |
| 40 | + StringIO(data), quoting='foo') |
| 41 | + |
| 42 | + # quoting must in the range [0, 3] |
| 43 | + msg = 'bad "quoting" value' |
| 44 | + tm.assertRaisesRegexp(TypeError, msg, self.read_csv, |
| 45 | + StringIO(data), quoting=5) |
| 46 | + |
| 47 | + def test_quote_char_basic(self): |
| 48 | + data = 'a,b,c\n1,2,"cat"' |
| 49 | + expected = DataFrame([[1, 2, 'cat']], |
| 50 | + columns=['a', 'b', 'c']) |
| 51 | + result = self.read_csv(StringIO(data), quotechar='"') |
| 52 | + tm.assert_frame_equal(result, expected) |
| 53 | + |
| 54 | + def test_quote_char_various(self): |
| 55 | + data = 'a,b,c\n1,2,"cat"' |
| 56 | + expected = DataFrame([[1, 2, 'cat']], |
| 57 | + columns=['a', 'b', 'c']) |
| 58 | + quote_chars = ['~', '*', '%', '$', '@', 'P'] |
| 59 | + |
| 60 | + for quote_char in quote_chars: |
| 61 | + new_data = data.replace('"', quote_char) |
| 62 | + result = self.read_csv(StringIO(new_data), quotechar=quote_char) |
| 63 | + tm.assert_frame_equal(result, expected) |
| 64 | + |
| 65 | + def test_null_quote_char(self): |
| 66 | + data = 'a,b,c\n1,2,3' |
| 67 | + |
| 68 | + # sanity checks |
| 69 | + msg = 'quotechar must be set if quoting enabled' |
| 70 | + |
| 71 | + tm.assertRaisesRegexp(TypeError, msg, self.read_csv, |
| 72 | + StringIO(data), quotechar=None, |
| 73 | + quoting=csv.QUOTE_MINIMAL) |
| 74 | + |
| 75 | + tm.assertRaisesRegexp(TypeError, msg, self.read_csv, |
| 76 | + StringIO(data), quotechar='', |
| 77 | + quoting=csv.QUOTE_MINIMAL) |
| 78 | + |
| 79 | + # no errors should be raised if quoting is None |
| 80 | + expected = DataFrame([[1, 2, 3]], |
| 81 | + columns=['a', 'b', 'c']) |
| 82 | + |
| 83 | + result = self.read_csv(StringIO(data), quotechar=None, |
| 84 | + quoting=csv.QUOTE_NONE) |
| 85 | + tm.assert_frame_equal(result, expected) |
| 86 | + |
| 87 | + result = self.read_csv(StringIO(data), quotechar='', |
| 88 | + quoting=csv.QUOTE_NONE) |
| 89 | + tm.assert_frame_equal(result, expected) |
| 90 | + |
| 91 | + def test_quoting_various(self): |
| 92 | + data = '1,2,"foo"' |
| 93 | + cols = ['a', 'b', 'c'] |
| 94 | + |
| 95 | + # QUOTE_MINIMAL and QUOTE_ALL apply only to |
| 96 | + # the CSV writer, so they should have no |
| 97 | + # special effect for the CSV reader |
| 98 | + expected = DataFrame([[1, 2, 'foo']], columns=cols) |
| 99 | + |
| 100 | + # test default (afterwards, arguments are all explicit) |
| 101 | + result = self.read_csv(StringIO(data), names=cols) |
| 102 | + tm.assert_frame_equal(result, expected) |
| 103 | + |
| 104 | + result = self.read_csv(StringIO(data), quotechar='"', |
| 105 | + quoting=csv.QUOTE_MINIMAL, names=cols) |
| 106 | + tm.assert_frame_equal(result, expected) |
| 107 | + |
| 108 | + result = self.read_csv(StringIO(data), quotechar='"', |
| 109 | + quoting=csv.QUOTE_ALL, names=cols) |
| 110 | + tm.assert_frame_equal(result, expected) |
| 111 | + |
| 112 | + # QUOTE_NONE tells the reader to do no special handling |
| 113 | + # of quote characters and leave them alone |
| 114 | + expected = DataFrame([[1, 2, '"foo"']], columns=cols) |
| 115 | + result = self.read_csv(StringIO(data), quotechar='"', |
| 116 | + quoting=csv.QUOTE_NONE, names=cols) |
| 117 | + tm.assert_frame_equal(result, expected) |
| 118 | + |
| 119 | + # QUOTE_NONNUMERIC tells the reader to cast |
| 120 | + # all non-quoted fields to float |
| 121 | + expected = DataFrame([[1.0, 2.0, 'foo']], columns=cols) |
| 122 | + result = self.read_csv(StringIO(data), quotechar='"', |
| 123 | + quoting=csv.QUOTE_NONNUMERIC, |
| 124 | + names=cols) |
| 125 | + tm.assert_frame_equal(result, expected) |
| 126 | + |
| 127 | + def test_double_quote(self): |
| 128 | + data = 'a,b\n3,"4 "" 5"' |
| 129 | + |
| 130 | + expected = DataFrame([[3, '4 " 5']], |
| 131 | + columns=['a', 'b']) |
| 132 | + result = self.read_csv(StringIO(data), quotechar='"', |
| 133 | + doublequote=True) |
| 134 | + tm.assert_frame_equal(result, expected) |
| 135 | + |
| 136 | + expected = DataFrame([[3, '4 " 5"']], |
| 137 | + columns=['a', 'b']) |
| 138 | + result = self.read_csv(StringIO(data), quotechar='"', |
| 139 | + doublequote=False) |
| 140 | + tm.assert_frame_equal(result, expected) |
0 commit comments