19
19
from pandas .io .parsers import (read_csv , read_table , read_fwf ,
20
20
TextFileReader , TextParser )
21
21
from pandas .util .testing import (assert_almost_equal ,
22
- assert_series_equal , network )
22
+ assert_series_equal ,
23
+ network ,
24
+ ensure_clean )
23
25
import pandas .util .testing as tm
24
26
import pandas as pd
25
27
@@ -1372,29 +1374,25 @@ def test_utf16_bom_skiprows(self):
1372
1374
1373
1375
path = '__%s__.csv' % tm .rands (10 )
1374
1376
1375
- for sep , dat in [('\t ' , data ), (',' , data2 )]:
1376
- for enc in ['utf-16' , 'utf-16le' , 'utf-16be' ]:
1377
- bytes = dat .encode (enc )
1378
- with open (path , 'wb' ) as f :
1379
- f .write (bytes )
1380
-
1381
- s = BytesIO (dat .encode ('utf-8' ))
1382
- if py3compat .PY3 :
1383
- # somewhat False since the code never sees bytes
1384
- from io import TextIOWrapper
1385
- s = TextIOWrapper (s , encoding = 'utf-8' )
1386
-
1387
- result = self .read_csv (path , encoding = enc , skiprows = 2 ,
1388
- sep = sep )
1389
- expected = self .read_csv (s , encoding = 'utf-8' , skiprows = 2 ,
1390
- sep = sep )
1391
-
1392
- tm .assert_frame_equal (result , expected )
1393
-
1394
- try :
1395
- os .remove (path )
1396
- except os .error :
1397
- pass
1377
+ with ensure_clean (path ) as path :
1378
+ for sep , dat in [('\t ' , data ), (',' , data2 )]:
1379
+ for enc in ['utf-16' , 'utf-16le' , 'utf-16be' ]:
1380
+ bytes = dat .encode (enc )
1381
+ with open (path , 'wb' ) as f :
1382
+ f .write (bytes )
1383
+
1384
+ s = BytesIO (dat .encode ('utf-8' ))
1385
+ if py3compat .PY3 :
1386
+ # somewhat False since the code never sees bytes
1387
+ from io import TextIOWrapper
1388
+ s = TextIOWrapper (s , encoding = 'utf-8' )
1389
+
1390
+ result = self .read_csv (path , encoding = enc , skiprows = 2 ,
1391
+ sep = sep )
1392
+ expected = self .read_csv (s , encoding = 'utf-8' , skiprows = 2 ,
1393
+ sep = sep )
1394
+
1395
+ tm .assert_frame_equal (result , expected )
1398
1396
1399
1397
def test_utf16_example (self ):
1400
1398
path = os .path .join (self .dirpath , 'utf16_ex.txt' )
@@ -1722,32 +1720,27 @@ def test_iteration_open_handle(self):
1722
1720
if PY3 :
1723
1721
raise nose .SkipTest
1724
1722
1725
- with open ('__foo__.txt' , 'wb' ) as f :
1726
- f .write ('AAA\n BBB\n CCC\n DDD\n EEE\n FFF\n GGG' )
1723
+ with ensure_clean () as path :
1724
+ with open (path , 'wb' ) as f :
1725
+ f .write ('AAA\n BBB\n CCC\n DDD\n EEE\n FFF\n GGG' )
1727
1726
1728
- with open ('__foo__.txt' , 'rb' ) as f :
1729
- for line in f :
1730
- if 'CCC' in line :
1731
- break
1727
+ with open (path , 'rb' ) as f :
1728
+ for line in f :
1729
+ if 'CCC' in line :
1730
+ break
1732
1731
1733
- try :
1734
- read_table (f , squeeze = True , header = None , engine = 'c' )
1735
- except Exception :
1736
- pass
1737
- else :
1738
- raise ValueError ('this should not happen' )
1739
-
1740
- result = read_table (f , squeeze = True , header = None ,
1741
- engine = 'python' )
1732
+ try :
1733
+ read_table (f , squeeze = True , header = None , engine = 'c' )
1734
+ except Exception :
1735
+ pass
1736
+ else :
1737
+ raise ValueError ('this should not happen' )
1742
1738
1743
- expected = Series (['DDD' , 'EEE' , 'FFF' , 'GGG' ])
1744
- tm .assert_series_equal (result , expected )
1745
-
1746
- try :
1747
- os .remove ('__foo__.txt' )
1748
- except os .error :
1749
- pass
1739
+ result = read_table (f , squeeze = True , header = None ,
1740
+ engine = 'python' )
1750
1741
1742
+ expected = Series (['DDD' , 'EEE' , 'FFF' , 'GGG' ])
1743
+ tm .assert_series_equal (result , expected )
1751
1744
1752
1745
class TestCParserHighMemory (ParserTests , unittest .TestCase ):
1753
1746
@@ -1924,41 +1917,30 @@ def test_decompression(self):
1924
1917
data = open (self .csv1 , 'rb' ).read ()
1925
1918
expected = self .read_csv (self .csv1 )
1926
1919
1927
- try :
1928
- tmp = gzip .GzipFile ('__tmp__' , mode = 'wb' )
1920
+ with ensure_clean () as path :
1921
+ tmp = gzip .GzipFile (path , mode = 'wb' )
1929
1922
tmp .write (data )
1930
1923
tmp .close ()
1931
1924
1932
- result = self .read_csv ('__tmp__' , compression = 'gzip' )
1925
+ result = self .read_csv (path , compression = 'gzip' )
1933
1926
tm .assert_frame_equal (result , expected )
1934
1927
1935
- result = self .read_csv (open ('__tmp__' , 'rb' ), compression = 'gzip' )
1928
+ result = self .read_csv (open (path , 'rb' ), compression = 'gzip' )
1936
1929
tm .assert_frame_equal (result , expected )
1937
- finally :
1938
- # try:
1939
- # os.remove('__tmp__')
1940
- # except:
1941
- # pass
1942
- pass
1943
1930
1944
- try :
1945
- tmp = bz2 .BZ2File ('__tmp__' , mode = 'wb' )
1931
+ with ensure_clean () as path :
1932
+ tmp = bz2 .BZ2File (path , mode = 'wb' )
1946
1933
tmp .write (data )
1947
1934
tmp .close ()
1948
1935
1949
- result = self .read_csv ('__tmp__' , compression = 'bz2' )
1936
+ result = self .read_csv (path , compression = 'bz2' )
1950
1937
tm .assert_frame_equal (result , expected )
1951
1938
1952
- # result = self.read_csv(open('__tmp__' , 'rb'), compression='bz2')
1939
+ # result = self.read_csv(open(path , 'rb'), compression='bz2')
1953
1940
# tm.assert_frame_equal(result, expected)
1954
1941
1955
1942
self .assertRaises (ValueError , self .read_csv ,
1956
- '__tmp__' , compression = 'bz3' )
1957
- finally :
1958
- try :
1959
- os .remove ('__tmp__' )
1960
- except :
1961
- pass
1943
+ path , compression = 'bz3' )
1962
1944
1963
1945
def test_decompression_regex_sep (self ):
1964
1946
try :
@@ -1971,35 +1953,24 @@ def test_decompression_regex_sep(self):
1971
1953
data = data .replace (b',' , b'::' )
1972
1954
expected = self .read_csv (self .csv1 )
1973
1955
1974
- try :
1975
- tmp = gzip .GzipFile ('__tmp__' , mode = 'wb' )
1956
+ with ensure_clean () as path :
1957
+ tmp = gzip .GzipFile (path , mode = 'wb' )
1976
1958
tmp .write (data )
1977
1959
tmp .close ()
1978
1960
1979
- result = self .read_csv ('__tmp__' , sep = '::' , compression = 'gzip' )
1961
+ result = self .read_csv (path , sep = '::' , compression = 'gzip' )
1980
1962
tm .assert_frame_equal (result , expected )
1981
- finally :
1982
- # try:
1983
- # os.remove('__tmp__')
1984
- # except:
1985
- # pass
1986
- pass
1987
1963
1988
- try :
1989
- tmp = bz2 .BZ2File ('__tmp__' , mode = 'wb' )
1964
+ with ensure_clean () as path :
1965
+ tmp = bz2 .BZ2File (path , mode = 'wb' )
1990
1966
tmp .write (data )
1991
1967
tmp .close ()
1992
1968
1993
- result = self .read_csv ('__tmp__' , sep = '::' , compression = 'bz2' )
1969
+ result = self .read_csv (path , sep = '::' , compression = 'bz2' )
1994
1970
tm .assert_frame_equal (result , expected )
1995
1971
1996
1972
self .assertRaises (ValueError , self .read_csv ,
1997
- '__tmp__' , compression = 'bz3' )
1998
- finally :
1999
- try :
2000
- os .remove ('__tmp__' )
2001
- except :
2002
- pass
1973
+ path , compression = 'bz3' )
2003
1974
2004
1975
def test_memory_map (self ):
2005
1976
# it works!
0 commit comments