Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/mixpanel_utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1851,7 +1851,7 @@ def _write_items_to_csv(items, output_file):
header.append(key)

# Create the writer and write the header
with open(output_file, "w", encoding="utf-8") as output:
with open(output_file, "w", encoding="utf-8", newline="") as output:
writer = csv.writer(output)

writer.writerow(header)
Expand Down
36 changes: 36 additions & 0 deletions tests/test_write_items_to_csv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import builtins
import csv

import mixpanel_utils
from mixpanel_utils import MixpanelUtils


def test_write_items_to_csv_does_not_add_blank_rows_with_windows_line_endings(
tmp_path, monkeypatch
):
# Simulate Windows text mode, where newline=None translates "\n" to "\r\n".
real_open = builtins.open

def windows_open(file, mode="r", *args, newline=None, **kwargs):
if "b" not in mode and newline is None:
newline = "\r\n"
return real_open(file, mode, *args, newline=newline, **kwargs)

monkeypatch.setattr(mixpanel_utils, "open", windows_open, raising=False)

output_file = tmp_path / "events.csv"
items = [
{"event": "Signup", "properties": {"distinct_id": "a", "time": 1}},
{"event": "Login", "properties": {"distinct_id": "b", "time": 2}},
]

MixpanelUtils._write_items_to_csv(items, str(output_file))

assert b"\r\r\n" not in output_file.read_bytes()
with open(output_file, newline="", encoding="utf-8") as f:
rows = list(csv.reader(f))
assert rows == [
["event", "distinct_id", "time"],
["Signup", "a", "1"],
["Login", "b", "2"],
]
Loading