[Solved] CSV.write(), file opens correctly in excel, but wrong layout in text

Hi, I hope someone can help me here, I’ve been banging away at this for a day now. I’m using CSV.write() to write a dataframe to a CSV file (.txt). The output opens perfectly in excel, but the layout isn’t right in the text file. I’m doing the following:

CSV.write(“test.txt”,df,writeheader=false,newline=“\n”)

For example, what i get in excel is in columns as expected:
a b c d
e f g h
i j k l
What appears in the text file is:
a,b,c,de,f,g,hi,j,k,l
I want it to appear in the text file as:
a,b,c,d
e,f,g,h
i, j,k,l
when I open it in notepad (I need it in this format).

I’ve tried opening a text file and using append, and I get the same result. I’ve searched all over, and there is lots of info about reading, but very little about writing. The help seems quite sparse too. ‘opts’ is mentioned, but no other information on ‘opts’.

I’m stumped, help appreciated.
Thanks

edit: Added commas to desired text file layout

Problem solved - newline changed to “\r\n”. Suspect I made the change, saved, but file was saved to different directory.

I never specify the newline when using CSV.write and I’ve not had this issue. Just tested to confirm that in both VS Code and when I just cat to the terminal, I get tabular looking output as you want. I wonder if the issue is more to do with whatever editor you are using and less to do with the actual file.

3 Likes