@@ -1669,10 +1669,10 @@ def test_to_latex(self):
1669
1669
\end{tabular}
1670
1670
"""
1671
1671
self .assertEqual (withoutindex_result , withoutindex_expected )
1672
-
1672
+
1673
1673
def test_to_latex_escape_special_chars (self ):
1674
1674
special_characters = ['&' ,'%' ,'$' ,'#' ,'_' ,
1675
- '{' ,'}' ,'~' ,'^' ,'\\ ' ]
1675
+ '{' ,'}' ,'~' ,'^' ,'\\ ' ]
1676
1676
df = DataFrame (data = special_characters )
1677
1677
observed = df .to_latex ()
1678
1678
expected = r"""\begin{tabular}{ll}
@@ -1694,6 +1694,99 @@ def test_to_latex_escape_special_chars(self):
1694
1694
"""
1695
1695
self .assertEqual (observed , expected )
1696
1696
1697
+ def test_to_csv_quotechar (self ):
1698
+ df = DataFrame ({'col' : [1 ,2 ]})
1699
+ expected = """\
1700
+ "","col"
1701
+ "0","1"
1702
+ "1","2"
1703
+ """
1704
+ with tm .ensure_clean ('test.csv' ) as path :
1705
+ df .to_csv (path , quoting = 1 ) # 1=QUOTE_ALL
1706
+ with open (path , 'r' ) as f :
1707
+ self .assertEqual (f .read (), expected )
1708
+ with tm .ensure_clean ('test.csv' ) as path :
1709
+ df .to_csv (path , quoting = 1 , engine = 'python' )
1710
+ with open (path , 'r' ) as f :
1711
+ self .assertEqual (f .read (), expected )
1712
+
1713
+ expected = """\
1714
+ $$,$col$
1715
+ $0$,$1$
1716
+ $1$,$2$
1717
+ """
1718
+ with tm .ensure_clean ('test.csv' ) as path :
1719
+ df .to_csv (path , quoting = 1 , quotechar = "$" )
1720
+ with open (path , 'r' ) as f :
1721
+ self .assertEqual (f .read (), expected )
1722
+ with tm .ensure_clean ('test.csv' ) as path :
1723
+ df .to_csv (path , quoting = 1 , quotechar = "$" , engine = 'python' )
1724
+ with open (path , 'r' ) as f :
1725
+ self .assertEqual (f .read (), expected )
1726
+
1727
+ with tm .ensure_clean ('test.csv' ) as path :
1728
+ with tm .assertRaisesRegexp (TypeError , 'quotechar' ):
1729
+ df .to_csv (path , quoting = 1 , quotechar = None )
1730
+ with tm .ensure_clean ('test.csv' ) as path :
1731
+ with tm .assertRaisesRegexp (TypeError , 'quotechar' ):
1732
+ df .to_csv (path , quoting = 1 , quotechar = None , engine = 'python' )
1733
+
1734
+ def test_to_csv_doublequote (self ):
1735
+ df = DataFrame ({'col' : ['a"a' , '"bb"' ]})
1736
+ expected = '''\
1737
+ "","col"
1738
+ "0","a""a"
1739
+ "1","""bb"""
1740
+ '''
1741
+ with tm .ensure_clean ('test.csv' ) as path :
1742
+ df .to_csv (path , quoting = 1 , doublequote = True ) # QUOTE_ALL
1743
+ with open (path , 'r' ) as f :
1744
+ self .assertEqual (f .read (), expected )
1745
+ with tm .ensure_clean ('test.csv' ) as path :
1746
+ df .to_csv (path , quoting = 1 , doublequote = True , engine = 'python' )
1747
+ with open (path , 'r' ) as f :
1748
+ self .assertEqual (f .read (), expected )
1749
+
1750
+ from _csv import Error
1751
+ with tm .ensure_clean ('test.csv' ) as path :
1752
+ with tm .assertRaisesRegexp (Error , 'escapechar' ):
1753
+ df .to_csv (path , doublequote = False ) # no escapechar set
1754
+ with tm .ensure_clean ('test.csv' ) as path :
1755
+ with tm .assertRaisesRegexp (Error , 'escapechar' ):
1756
+ df .to_csv (path , doublequote = False , engine = 'python' )
1757
+
1758
+ def test_to_csv_escapechar (self ):
1759
+ df = DataFrame ({'col' : ['a"a' , '"bb"' ]})
1760
+ expected = """\
1761
+ "","col"
1762
+ "0","a\\ "a"
1763
+ "1","\\ "bb\\ ""
1764
+ """
1765
+ with tm .ensure_clean ('test.csv' ) as path : # QUOTE_ALL
1766
+ df .to_csv (path , quoting = 1 , doublequote = False , escapechar = '\\ ' )
1767
+ with open (path , 'r' ) as f :
1768
+ self .assertEqual (f .read (), expected )
1769
+ with tm .ensure_clean ('test.csv' ) as path :
1770
+ df .to_csv (path , quoting = 1 , doublequote = False , escapechar = '\\ ' ,
1771
+ engine = 'python' )
1772
+ with open (path , 'r' ) as f :
1773
+ self .assertEqual (f .read (), expected )
1774
+
1775
+ df = DataFrame ({'col' : ['a,a' , ',bb,' ]})
1776
+ expected = """\
1777
+ ,col
1778
+ 0,a\\ ,a
1779
+ 1,\\ ,bb\\ ,
1780
+ """
1781
+ with tm .ensure_clean ('test.csv' ) as path :
1782
+ df .to_csv (path , quoting = 3 , escapechar = '\\ ' ) # QUOTE_NONE
1783
+ with open (path , 'r' ) as f :
1784
+ self .assertEqual (f .read (), expected )
1785
+ with tm .ensure_clean ('test.csv' ) as path :
1786
+ df .to_csv (path , quoting = 3 , escapechar = '\\ ' , engine = 'python' )
1787
+ with open (path , 'r' ) as f :
1788
+ self .assertEqual (f .read (), expected )
1789
+
1697
1790
class TestSeriesFormatting (tm .TestCase ):
1698
1791
_multiprocess_can_split_ = True
1699
1792
0 commit comments