1
1
# -*- coding: utf-8 -*-
2
2
3
3
import sys
4
+ import os
4
5
import numpy as np
5
6
import pandas as pd
6
7
import pytest
@@ -294,20 +295,33 @@ def test_to_csv_string_with_lf(self):
294
295
}
295
296
df = pd .DataFrame (data )
296
297
298
+ os_linesep = os .linesep .encode ('utf-8' )
299
+
297
300
with tm .ensure_clean ('lf_test.csv' ) as path :
298
- expected_bin = (
301
+ expected_noarg = (
302
+ b'int,str_lf' + os_linesep +
303
+ b'1,abc' + os_linesep +
304
+ b'2,"d\n ef"' + os_linesep +
305
+ b'3,"g\n h\n \n i"' + os_linesep
306
+ )
307
+
308
+ df .to_csv (path , index = False )
309
+ with open (path , 'rb' ) as f :
310
+ assert f .read () == expected_noarg
311
+
312
+ expected_lf = (
299
313
b'int,str_lf\n '
300
314
b'1,abc\n '
301
315
b'2,"d\n ef"\n '
302
316
b'3,"g\n h\n \n i"\n '
303
317
)
304
318
305
- df .to_csv (path , index = False )
319
+ df .to_csv (path , line_terminator = ' \n ' , index = False )
306
320
with open (path , 'rb' ) as f :
307
- assert f .read () == expected_bin
321
+ assert f .read () == expected_lf
308
322
309
323
# 'line_terminator' should not change inner element
310
- expected_bin = (
324
+ expected_crlf = (
311
325
b'int,str_lf\r \n '
312
326
b'1,abc\r \n '
313
327
b'2,"d\n ef"\r \n '
@@ -316,7 +330,7 @@ def test_to_csv_string_with_lf(self):
316
330
317
331
df .to_csv (path , line_terminator = '\r \n ' , index = False )
318
332
with open (path , 'rb' ) as f :
319
- assert f .read () == expected_bin
333
+ assert f .read () == expected_crlf
320
334
321
335
def test_to_csv_string_with_crlf (self ):
322
336
# GH 20353
@@ -326,19 +340,32 @@ def test_to_csv_string_with_crlf(self):
326
340
}
327
341
df = pd .DataFrame (data )
328
342
343
+ os_linesep = os .linesep .encode ('utf-8' )
344
+
329
345
with tm .ensure_clean ('crlf_test.csv' ) as path :
330
- expected_bin = (
346
+ expected_noarg = (
347
+ b'int,str_crlf' + os_linesep +
348
+ b'1,abc' + os_linesep +
349
+ b'2,"d\r \n ef"' + os_linesep +
350
+ b'3,"g\r \n h\r \n \r \n i"' + os_linesep
351
+ )
352
+
353
+ df .to_csv (path , index = False )
354
+ with open (path , 'rb' ) as f :
355
+ assert f .read () == expected_noarg
356
+
357
+ expected_lf = (
331
358
b'int,str_crlf\n '
332
359
b'1,abc\n '
333
360
b'2,"d\r \n ef"\n '
334
361
b'3,"g\r \n h\r \n \r \n i"\n '
335
362
)
336
363
337
- df .to_csv (path , index = False )
364
+ df .to_csv (path , line_terminator = ' \n ' , index = False )
338
365
with open (path , 'rb' ) as f :
339
- assert f .read () == expected_bin
366
+ assert f .read () == expected_lf
340
367
341
- expected_bin = (
368
+ expected_crlf = (
342
369
b'int,str_crlf\r \n '
343
370
b'1,abc\r \n '
344
371
b'2,"d\r \n ef"\r \n '
@@ -347,4 +374,4 @@ def test_to_csv_string_with_crlf(self):
347
374
348
375
df .to_csv (path , line_terminator = '\r \n ' , index = False )
349
376
with open (path , 'rb' ) as f :
350
- assert f .read () == expected_bin
377
+ assert f .read () == expected_crlf
0 commit comments