3

I have what I thought would be a simple task. I need to create a text file that has uses the unix LF convention at the end of a line. However, when I attempt to accomplish this using pandas .to_csv, I end up with CR LF. This wouldn't be a problem if I were staying on my machine and if I were using python for everything. But I am not. The code will be part of an app that a coworker will be using, and the file is being sent to a third party for automation. The code is as follows:

df.to_csv("filename.txt", sep = '\t',line_terminator = '\n')

This sits in the middle of my code, but it always switches back to CR LF when the files is saved in the target folder. I think it may be something to do with Windows being "helpful". I have tried to use this type of solution with no luck. I am also not having any luck finding a dos2unix type package for Python. Is there anyway that I can write lines that end in '\n' and have it stick in Windows (using to_csv if possible)?

I am using Python 2.7 and Windows 7.

Edit: I know about notepad++, but I am trying to write this all in Python, so my coworker only has to click a button (I will be using Tkinter and freezing this to an .exe once I am finished). The .txt can be Windows formatted, but I will be (am) writing the same information to a .MAT and .ref file. In those files it needs the '\n' end of line, but Windows is still giving me '\r\n'.

Mark Tolonen
  • 132,868
  • 21
  • 152
  • 208
Josh
  • 33
  • 7
  • I suggest you try to write the filename without an extension that is familiar to Windows (.txt) maybe just not have an extension. It doesn't matter to linux anyways. – Saher Ahwal Dec 28 '16 at 21:18
  • @SaherAhwal: the file extension has nothing to do with the problem, the problem is with the data _in_ the file. – Bryan Oakley Dec 29 '16 at 01:19

1 Answers1

5

Note this answer is specific to Python 2's CSV handling.

to_csv defaults to opening the file in 'w' mode which defaults to text mode. On Windows text mode translates \n to \r\n. Open the the file in binary mode instead, and specify an encoding if you have non-ASCII text in your frame.

df.to_csv('filename.txt',sep='\t',mode='wb',encoding='utf8')
Mark Tolonen
  • 132,868
  • 21
  • 152
  • 208