Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

io.metadata: Fix newline handling when reading CSV/TSV #1561

Merged
merged 2 commits into from
Jul 30, 2024
Merged
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
7 changes: 7 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

## __NEXT__

### Bug Fixes

* Embedded newlines in quoted field values of metadata files are now properly handled. [#1561][] (@tsibley)

[#1561]: https://github.com/nextstrain/augur/pull/1561



## 25.2.0 (24 July 2024)

Expand Down
4 changes: 2 additions & 2 deletions augur/io/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,7 @@ def __init__(self, path: str, delimiters: Sequence[str], id_columns: Sequence[st

def open(self, **kwargs):
"""Open the file with auto-compression/decompression."""
return open_file(self.path, **kwargs)
return open_file(self.path, newline='', **kwargs)

def _find_first(self, columns: Sequence[str]):
"""Return the first column in `columns` that is present in the metadata.
Expand Down Expand Up @@ -646,7 +646,7 @@ def _get_delimiter(path: str, valid_delimiters: Iterable[str]):
if len(delimiter) != 1:
raise AugurError(f"Delimiters must be single-character strings. {delimiter!r} does not satisfy that condition.")

with open_file(path) as file:
with open_file(path, newline='') as file:
tsibley marked this conversation as resolved.
Show resolved Hide resolved
try:
# Infer the delimiter from the first line.
return csv.Sniffer().sniff(file.readline(), "".join(valid_delimiters)).delimiter
Expand Down
19 changes: 18 additions & 1 deletion tests/io/test_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ def test_write_records_to_tsv_with_empty_records(self, tmpdir):

def write_lines(tmpdir, lines):
path = str(tmpdir / "tmp")
with open(path, 'w') as f:
with open(path, 'w', newline='') as f:
f.writelines(lines)
return path

Expand Down Expand Up @@ -603,3 +603,20 @@ def test_rows_strict_missing(self, tmpdir):
{'a': '2', 'b': '2', 'c': ''},
{'a': '3', 'b': '2', 'c': None},
]

def test_rows_embedded_newline(self, tmpdir):
"""Test behavior when reading rows with an embedded newline."""
path = write_lines(tmpdir, [
'a,b,c\n',
'1,2,3\n',
'4,"5\r\n6",7\n',
'8,9,10\n',
])

m = Metadata(path, delimiters=',', id_columns=['a'])

assert list(m.rows()) == [
{'a': '1', 'b': '2', 'c': '3'},
{'a': '4', 'b': '5\r\n6', 'c': '7'},
{'a': '8', 'b': '9', 'c': '10'},
]
Loading