test_quoting.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. # -*- coding: utf-8 -*-
  2. """
  3. Tests that quoting specifications are properly handled
  4. during parsing for all of the parsers defined in parsers.py
  5. """
  6. import csv
  7. import pytest
  8. from pandas.compat import PY2, StringIO, u
  9. from pandas.errors import ParserError
  10. from pandas import DataFrame
  11. import pandas.util.testing as tm
  12. @pytest.mark.parametrize("kwargs,msg", [
  13. (dict(quotechar="foo"), '"quotechar" must be a(n)? 1-character string'),
  14. (dict(quotechar=None, quoting=csv.QUOTE_MINIMAL),
  15. "quotechar must be set if quoting enabled"),
  16. (dict(quotechar=2), '"quotechar" must be string, not int')
  17. ])
  18. def test_bad_quote_char(all_parsers, kwargs, msg):
  19. data = "1,2,3"
  20. parser = all_parsers
  21. with pytest.raises(TypeError, match=msg):
  22. parser.read_csv(StringIO(data), **kwargs)
  23. @pytest.mark.parametrize("quoting,msg", [
  24. ("foo", '"quoting" must be an integer'),
  25. (5, 'bad "quoting" value'), # quoting must be in the range [0, 3]
  26. ])
  27. def test_bad_quoting(all_parsers, quoting, msg):
  28. data = "1,2,3"
  29. parser = all_parsers
  30. with pytest.raises(TypeError, match=msg):
  31. parser.read_csv(StringIO(data), quoting=quoting)
  32. def test_quote_char_basic(all_parsers):
  33. parser = all_parsers
  34. data = 'a,b,c\n1,2,"cat"'
  35. expected = DataFrame([[1, 2, "cat"]],
  36. columns=["a", "b", "c"])
  37. result = parser.read_csv(StringIO(data), quotechar='"')
  38. tm.assert_frame_equal(result, expected)
  39. @pytest.mark.parametrize("quote_char", ["~", "*", "%", "$", "@", "P"])
  40. def test_quote_char_various(all_parsers, quote_char):
  41. parser = all_parsers
  42. expected = DataFrame([[1, 2, "cat"]],
  43. columns=["a", "b", "c"])
  44. data = 'a,b,c\n1,2,"cat"'
  45. new_data = data.replace('"', quote_char)
  46. result = parser.read_csv(StringIO(new_data), quotechar=quote_char)
  47. tm.assert_frame_equal(result, expected)
  48. @pytest.mark.parametrize("quoting", [csv.QUOTE_MINIMAL, csv.QUOTE_NONE])
  49. @pytest.mark.parametrize("quote_char", ["", None])
  50. def test_null_quote_char(all_parsers, quoting, quote_char):
  51. kwargs = dict(quotechar=quote_char, quoting=quoting)
  52. data = "a,b,c\n1,2,3"
  53. parser = all_parsers
  54. if quoting != csv.QUOTE_NONE:
  55. # Sanity checking.
  56. msg = "quotechar must be set if quoting enabled"
  57. with pytest.raises(TypeError, match=msg):
  58. parser.read_csv(StringIO(data), **kwargs)
  59. else:
  60. expected = DataFrame([[1, 2, 3]], columns=["a", "b", "c"])
  61. result = parser.read_csv(StringIO(data), **kwargs)
  62. tm.assert_frame_equal(result, expected)
  63. @pytest.mark.parametrize("kwargs,exp_data", [
  64. (dict(), [[1, 2, "foo"]]), # Test default.
  65. # QUOTE_MINIMAL only applies to CSV writing, so no effect on reading.
  66. (dict(quotechar='"', quoting=csv.QUOTE_MINIMAL), [[1, 2, "foo"]]),
  67. # QUOTE_MINIMAL only applies to CSV writing, so no effect on reading.
  68. (dict(quotechar='"', quoting=csv.QUOTE_ALL), [[1, 2, "foo"]]),
  69. # QUOTE_NONE tells the reader to do no special handling
  70. # of quote characters and leave them alone.
  71. (dict(quotechar='"', quoting=csv.QUOTE_NONE), [[1, 2, '"foo"']]),
  72. # QUOTE_NONNUMERIC tells the reader to cast
  73. # all non-quoted fields to float
  74. (dict(quotechar='"', quoting=csv.QUOTE_NONNUMERIC), [[1.0, 2.0, "foo"]])
  75. ])
  76. def test_quoting_various(all_parsers, kwargs, exp_data):
  77. data = '1,2,"foo"'
  78. parser = all_parsers
  79. columns = ["a", "b", "c"]
  80. result = parser.read_csv(StringIO(data), names=columns, **kwargs)
  81. expected = DataFrame(exp_data, columns=columns)
  82. tm.assert_frame_equal(result, expected)
  83. @pytest.mark.parametrize("doublequote,exp_data", [
  84. (True, [[3, '4 " 5']]),
  85. (False, [[3, '4 " 5"']]),
  86. ])
  87. def test_double_quote(all_parsers, doublequote, exp_data):
  88. parser = all_parsers
  89. data = 'a,b\n3,"4 "" 5"'
  90. result = parser.read_csv(StringIO(data), quotechar='"',
  91. doublequote=doublequote)
  92. expected = DataFrame(exp_data, columns=["a", "b"])
  93. tm.assert_frame_equal(result, expected)
  94. @pytest.mark.parametrize("quotechar", [
  95. u('"'),
  96. pytest.param(u('\u0001'), marks=pytest.mark.skipif(
  97. PY2, reason="Python 2.x does not handle unicode well."))])
  98. def test_quotechar_unicode(all_parsers, quotechar):
  99. # see gh-14477
  100. data = "a\n1"
  101. parser = all_parsers
  102. expected = DataFrame({"a": [1]})
  103. result = parser.read_csv(StringIO(data), quotechar=quotechar)
  104. tm.assert_frame_equal(result, expected)
  105. @pytest.mark.parametrize("balanced", [True, False])
  106. def test_unbalanced_quoting(all_parsers, balanced):
  107. # see gh-22789.
  108. parser = all_parsers
  109. data = "a,b,c\n1,2,\"3"
  110. if balanced:
  111. # Re-balance the quoting and read in without errors.
  112. expected = DataFrame([[1, 2, 3]], columns=["a", "b", "c"])
  113. result = parser.read_csv(StringIO(data + '"'))
  114. tm.assert_frame_equal(result, expected)
  115. else:
  116. msg = ("EOF inside string starting at row 1" if parser.engine == "c"
  117. else "unexpected end of data")
  118. with pytest.raises(ParserError, match=msg):
  119. parser.read_csv(StringIO(data))