Skip to content
Open
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

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ def transform(self, input_stream: IO[bytes]) -> DictReader:
# S3Object re-creates its transformed stream), and each call must see the original options.
options = dict(self.transform_options)
encoding = options.pop("encoding", "utf-8")
newline = options.pop("newline", None)
# csv expects newline="" so that line breaks inside quoted fields are kept as they are
newline = options.pop("newline", "")

# csv module needs an Iterator[str], so we wrap the underlying stream into a TextIO
iterator = io.TextIOWrapper(input_stream, encoding=encoding, newline=newline)
Expand Down
3 changes: 3 additions & 0 deletions docs/utilities/streaming.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ We will propagate additional options to the underlying implementation for each t
| **ZipTransform** | [ZipFile constructor](https://docs.python.org/3/library/zipfile.html#zipfile.ZipFile){target="_blank" rel="nofollow"} |
| **CsvTransform** | [DictReader constructor](https://docs.python.org/3/library/csv.html#csv.DictReader){target="_blank" rel="nofollow"} |

`CsvTransform` also accepts `encoding` (default `utf-8`) and `newline` (default `""`), which it passes to the [TextIOWrapper](https://docs.python.org/3/library/io.html#io.TextIOWrapper){target="_blank" rel="nofollow"} that decodes the object.
With `newline=""`, line breaks inside quoted fields are kept as they are, as the [csv module](https://docs.python.org/3/library/csv.html#id4){target="_blank" rel="nofollow"} expects. Pass `newline=None` if you want them translated to `\n`.

For instance, take `ZipTransform`. You can use the `compression` parameter if you want to unzip an S3 object compressed with `LZMA`.

=== "Unzipping LZMA data"
Expand Down
12 changes: 12 additions & 0 deletions tests/functional/streaming/_boto3/test_s3_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,15 @@ def test_csv_transform_keeps_options_when_reused(s3_client, s3_client_stub):
assert rows == [{"name": "café", "city": "Zürich"}]

assert transform.transform_options == {"encoding": "latin-1", "delimiter": ";"}


def test_csv_transform_keeps_line_breaks_inside_quoted_fields(s3_client, s3_client_stub):
# GIVEN a CSV object whose quoted fields contain CRLF and a lone CR
payload = b'id,note\r\n1,"line one\r\nline two"\r\n2,"carriage\rreturn"\r\n'
_stub_get_object(s3_client_stub, payload)

# WHEN it is read with the default CsvTransform
rows = list(S3Object(bucket="bucket", key="key", boto3_client=s3_client).transform(CsvTransform()))

# THEN the quoted fields keep their original line breaks, as csv.DictReader does on a file opened with newline=""
assert rows == [{"id": "1", "note": "line one\r\nline two"}, {"id": "2", "note": "carriage\rreturn"}]