DelimitedFiles.jl icon indicating copy to clipboard operation
DelimitedFiles.jl copied to clipboard

writedlm - issues with line breaks on Windows

Open rowlesmr opened this issue 10 years ago • 5 comments

When writing an array with writedlm on Windows, lines are not terminated with /r/n.

julia> rnd = rand(2,2)
2x2 Array{Float64,2}:
 0.417898  0.534031
 0.593108  0.909771
julia> writedlm("out.txt", rnd)

-> .41789803012680227 .5340308732027403.5931077439922414 .9097713865838699

Expected behaviour: on a Windows system, the Windows line break would be used.

Julia Version 0.4.0
Commit 0ff703b* (2015-10-08 06:20 UTC)
Platform Info:
  System: Windows (x86_64-w64-mingw32)
  CPU: Intel(R) Core(TM) i7-4790 CPU @ 3.60GHz
  WORD_SIZE: 64
  BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY Haswell)
  LAPACK: libopenblas64_
  LIBM: libopenlibm
  LLVM: libLLVM-3.3

rowlesmr avatar Oct 13 '15 07:10 rowlesmr

I'd rather not, personally. Aside from notepad.exe, what programs can't handle unix line endings by now? If you really want \r\n, you can run(unix2dos out.txt)

tkelman avatar Oct 13 '15 07:10 tkelman

I just discovered RFC 4180 (the only one about CSV) recommends using CRLF line endings:

As per section 4.1.1. of RFC 2046 [3], this media type uses CRLF to denote line breaks. However, implementors should be aware that some implementations may use other values.

RFC 2046 is about MIME. It sounds too bad not to follow the standard here, but since no standard really exists and it is easy to support both kinds of line breaks, it's not the end of the world. FWIW, R uses \r\n on Windows, and \n elsewhere. At least, always doing the same thing on all platforms sounds like a good idea for Julia.

I guess the question is: does Excel open non-CRLF CSV files correctly by default?

nalimilan avatar Oct 13 '15 08:10 nalimilan

Excel does open non-CRLF CSV files correctly by default.

rowlesmr avatar Oct 13 '15 09:10 rowlesmr

Thanks. So is there any specific reason why you'd prefer CRLF?

nalimilan avatar Oct 13 '15 09:10 nalimilan

I use notepad to view my files as it is quick, light-weight, and doesn't lock files for editing.

Also, I think that it would be throw up the least number of surprises by doing Windows things on Windows computers.

rowlesmr avatar Oct 23 '15 00:10 rowlesmr