@@ -202,49 +202,32 @@ Backwards incompatible API changes
202
202
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
203
203
204
204
:func:`DataFrame.to_csv` now uses :func:`os.linesep` rather than ``'\n'``
205
- for the default line terminator(:issue:`20353`).
206
- - This change only affects when running on Windows, where ``'\r\n'`` was used for line terminator
205
+ for the default line terminator (:issue:`20353`).
206
+ This change only affects when running on Windows, where ``'\r\n'`` was used for line terminator
207
207
even when ``'\n'`` was passed in ``line_terminator``.
208
- - Strictly speeaking, all ``'\n'``s appear in data and line terminator of CSV were converted into ``'\r\n'``s.
209
- - This problem was resolved by passing file object with ``newline='\n'`` option as output, rather than file name.
210
208
211
209
Previous Behavior on Windows:
212
210
213
211
.. code-block:: ipython
214
212
215
- In [1]: import pandas as pd
213
+ In [1]: data = pd.DataFrame({
214
+ ...: "string_with_lf":["a\nbc"],
215
+ ...: "string_with_crlf":["a\r\nbc"]
216
+ ...: })
216
217
217
- In [2]: data = pd.DataFrame({
218
- ...: "string_with_lf":["abc","d\nef","g\nh\n\ni"],
219
- ...: "string_with_crlf":["abc","d\r\nef","g\r\nh\r\n\r\ni"]
220
- ...: })
218
+ In [2]: data.to_csv("test.csv",index=False,line_terminator='\n')
221
219
222
- In [3]: data.to_csv("test.csv",index=False,line_terminator='\n')
220
+ In [3]: with open("test.csv", mode='rb') as f:
221
+ ...: print(f.read())
222
+ b'string_with_lf,string_with_crlf\r\n"a\r\nbc","a\r\r\nbc"\r\n'
223
223
224
- In [4]: print(pd.read_csv("test.csv"))
225
- string_with_lf string_with_crlf
226
- 0 abc abc
227
- 1 d\r\nef d\r\r\nef
228
- 2 g\r\nh\r\n\r\ni g\r\r\nh\r\r\n\r\r\ni
224
+ In [4]: with open("test2.csv", mode='w', newline='\n') as f:
225
+ ...: data.to_csv(f,index=False,line_terminator='\n')
229
226
230
- In [5]: with open("test.csv", mode='rb') as f:
231
- ...: print(f.read())
232
- b'string_with_lf,string_with_crlf\r\nabc,abc\r\n"d\r\nef","d\r\r\nef"\r\n"g\r\nh
233
- \r\n\r\ni","g\r\r\nh\r\r\n\r\r\ni"\r\n'
227
+ In [5]: with open("test2.csv", mode='rb') as f:
228
+ ...: print(f.read())
229
+ b'string_with_lf,string_with_crlf\n"a\nbc","a\r\nbc"\n'
234
230
235
- In [6]: with open("test2.csv", mode='w', newline='\n') as f:
236
- ...: data.to_csv(f,index=False,line_terminator='\n')
237
-
238
- In [7]: print(pd.read_csv("test2.csv"))
239
- string_with_lf string_with_crlf
240
- 0 abc abc
241
- 1 d\nef d\r\nef
242
- 2 g\nh\n\ni g\r\nh\r\n\r\ni
243
-
244
- In [8]: with open("test2.csv", mode='rb') as f:
245
- ...: print(f.read())
246
- b'string_with_lf,string_with_crlf\nabc,abc\n"d\nef","d\r\nef"\n"g\nh\n\ni","g\r\
247
- nh\r\n\r\ni"\n'
248
231
249
232
New Behavior on Windows:
250
233
@@ -254,99 +237,52 @@ New Behavior on Windows:
254
237
255
238
.. code-block:: ipython
256
239
257
- In [1]: import pandas as pd
258
-
259
- In [2]: data = pd.DataFrame({
260
- ...: "string_with_lf":["abc","d\nef","g\nh\n\ni"],
261
- ...: "string_with_crlf":["abc","d\r\nef","g\r\nh\r\n\r\ni"]
262
- ...: })
240
+ In [1]: data = pd.DataFrame({
241
+ ...: "string_with_lf":["a\nbc"],
242
+ ...: "string_with_crlf":["a\r\nbc"]
243
+ ...: })
263
244
264
- In [3 ]: data.to_csv("test.csv",index=False,line_terminator='\n')
245
+ In [2 ]: data.to_csv("test.csv",index=False,line_terminator='\n')
265
246
266
- In [4]: pd.read_csv("test.csv")
267
- Out[4]:
268
- string_with_lf string_with_crlf
269
- 0 abc abc
270
- 1 d\nef d\r\nef
271
- 2 g\nh\n\ni g\r\nh\r\n\r\ni
247
+ In [3]: with open("test.csv", mode='rb') as f:
248
+ ...: print(f.read())
249
+ b'string_with_lf,string_with_crlf\n"a\nbc","a\r\nbc"\n'
272
250
273
- In [5]: with open("test.csv", mode='rb') as f:
274
- ...: binary_str=f.read()
275
- ...: binary_str
276
- Out[5]: b'string_with_lf,string_with_crlf\nabc,abc\n"d\nef","d\r\nef"\n"g\nh\n\n
277
- i","g\r\nh\r\n\r\ni"\n'
278
251
279
252
- On windows, the value of ``os.linesep`` is ``'\r\n'``,
280
253
so if ``line_terminator`` is not set, ``'\r\n'`` is used for line terminator.
281
254
- Again, it does not affects the value inside the data.
282
255
283
256
.. code-block:: ipython
284
257
285
- In [1]: import pandas as pd
258
+ In [1]: data = pd.DataFrame({
259
+ ...: "string_with_lf":["a\nbc"],
260
+ ...: "string_with_crlf":["a\r\nbc"]
261
+ ...: })
286
262
287
- In [2]: data = pd.DataFrame({
288
- ...: "string_with_lf":["abc","d\nef","g\nh\n\ni"],
289
- ...: "string_with_crlf":["abc","d\r\nef","g\r\nh\r\n\r\ni"]
290
- ...: })
263
+ In [2]: data.to_csv("test.csv",index=False)
291
264
292
- In [3]: data.to_csv("test.csv",index=False)
265
+ In [3]: with open("test.csv", mode='rb') as f:
266
+ ...: print(f.read())
267
+ b'string_with_lf,string_with_crlf\r\n"a\nbc","a\r\nbc"\r\n'
293
268
294
- In [4]: pd.read_csv("test.csv")
295
- Out[4]:
296
- string_with_lf string_with_crlf
297
- 0 abc abc
298
- 1 d\nef d\r\nef
299
- 2 g\nh\n\ni g\r\nh\r\n\r\ni
300
-
301
- In [5]: with open("test.csv", mode='rb') as f:
302
- ...: binary_str=f.read()
303
- ...: binary_str
304
- Out[5]: b'string_with_lf,string_with_crlf\r\nabc,abc\r\n"d\nef","d\r\nef"\r\n"g\
305
- nh\n\ni","g\r\nh\r\n\r\ni"\r\n'
306
269
307
270
- As default value of ``line_terminator`` changes, just passing file object with ``newline='\n'`` does not set ``'\n'`` to line terminator.
308
271
Pass ``line_terminator='\n'`` explicitly.
309
272
310
273
.. code-block:: ipython
311
274
312
- In [1]: import pandas as pd
313
-
314
- In [2]: data = pd.DataFrame({
315
- ...: "string_with_lf":["abc","d\nef","g\nh\n\ni"],
316
- ...: "string_with_crlf":["abc","d\r\nef","g\r\nh\r\n\r\ni"]
317
- ...: })
318
-
319
- In [3]: with open("test2.csv", mode='w', newline='\n') as f:
320
- ...: data.to_csv(f,index=False)
321
-
322
- In [4]: pd.read_csv("test2.csv")
323
- Out[4]:
324
- string_with_lf string_with_crlf
325
- 0 abc abc
326
- 1 d\nef d\r\nef
327
- 2 g\nh\n\ni g\r\nh\r\n\r\ni
328
-
329
- In [5]: with open("test2.csv", mode='rb') as f:
330
- ...: binary_str=f.read()
331
- ...: binary_str
332
- Out[5]: b'string_with_lf,string_with_crlf\r\nabc,abc\r\n"d\nef","d\r\nef"\r\n"g\
333
- nh\n\ni","g\r\nh\r\n\r\ni"\r\n'
334
-
335
- In [6]: with open("test2.csv", mode='w', newline='\n') as f:
336
- ...: data.to_csv(f,index=False,line_terminator='\n')
337
-
338
- In [7]: pd.read_csv("test2.csv")
339
- Out[7]:
340
- string_with_lf string_with_crlf
341
- 0 abc abc
342
- 1 d\nef d\r\nef
343
- 2 g\nh\n\ni g\r\nh\r\n\r\ni
344
-
345
- In [8]: with open("test2.csv", mode='rb') as f:
346
- ...: binary_str=f.read()
347
- ...: binary_str
348
- Out[8]: b'string_with_lf,string_with_crlf\nabc,abc\n"d\nef","d\r\nef"\n"g\nh\n\n
349
- i","g\r\nh\r\n\r\ni"\n'
275
+ In [1]: data = pd.DataFrame({
276
+ ...: "string_with_lf":["a\nbc"],
277
+ ...: "string_with_crlf":["a\r\nbc"]
278
+ ...: })
279
+
280
+ In [2]: with open("test2.csv", mode='w', newline='\n') as f:
281
+ ...: data.to_csv(f,index=False)
282
+
283
+ In [3]: with open("test2.csv", mode='rb') as f:
284
+ ...: print(f.read())
285
+ b'string_with_lf,string_with_crlf\r\n"a\nbc","a\r\nbc"\r\n'
350
286
351
287
352
288
.. _whatsnew_0240.api_breaking.interval_values:
0 commit comments