0

Excel 15 on MacOS Sierra isn't displaying line breaks for some reason.

I have some simple Python code that creates a csv

output = "test.csv"

sensor_names = ["A", "B", "C"]

data = [ [1,2,3], [4,5,6], None, [10,11,12] ]

with open(output, "wb", newline='') as fp:
    fp.write(",".join(sensor_names))
    fp.write("\n")

    for line in data:
        if line is None:
            fp.write("\n")
            continue

        fp.write(",".join(str(x) for x in line))
        fp.write("\n")

This code produces one line break, and when I open it with a text editor, I see a blank line. But when I open it in Excel, then the blank line doesn't show up.

If I put two new lines \n\n, then a blank shows up in Excel, but then there are actually two new lines in the actual file, which I don't want.

Also putting \r\n worked, but again, that adds an unwanted character since I'm not using Windows.

Any solutions to this?

rasen58
  • 4,672
  • 8
  • 39
  • 74

1 Answers1

0

It not an extra character but a much needed one if you want to have compatibility between different os (windows,mac and linux) Basically \r\n works for windows and \r or \n will work perfect for both linux and mac performing the same functionality.

For further reference take a look at this

Hope this helps!

Community
  • 1
  • 1
Keerthana Prabhakaran
  • 3,766
  • 1
  • 13
  • 23
  • I know about the differences, and in this case, I don't need compatibility. I just want to be able to see a new line in Excel on my Mac using only a \n because that's what line ending Macs use. – rasen58 May 18 '17 at 18:23