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'.
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.