Writing CSV data - #1
Merged
Merged
Conversation
lpheller
marked this pull request as draft
July 18, 2024 10:06
lpheller
force-pushed
the
feat/write-csv
branch
from
August 17, 2026 07:04
32f901f to
27012b8
Compare
lpheller
marked this pull request as ready for review
August 17, 2026 07:05
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds a writer.
withHeaders()writes a header row and fixes the column order, so associative rows land in the right columns no matter what order their keys are in, and a column a row doesn't carry is written empty instead of shifting everything left. Objects go out by their public properties, so whatevermapToObject()returns can be written straight back.delimiter(),escape()andcrlf()cover the format,bom()puts a UTF-8 BOM in front. Excel on Windows wants all three:insertAt()is the only one that touches an existing file, so it takes some care:fgetcsv, so a newline inside a quoted field doesn't shift the positionstream_copy_to_streamand stays byte for byte identical, no reformattingPositions count records from 1 and the header is record 1, so the first data row is position 2.
Also in here, on the read side
Csv::read()->encoding('Windows-1252')converts while reading, as a stream filter, so it costs about 10% on a million rows. Without it non-ASCII comes back as invalid UTF-8 andtoJson()throws on it.Behaviour change: fields are now parsed per RFC 4180, where a backslash is just a character. Previously a field ending in one swallowed its closing quote and ate the rest of the line.
escape('\\')brings the old behaviour back for files that need it, such as a MySQLSELECT ... INTO OUTFILEdump. Worth a minor bump.Housekeeping
This force-pushes over the old WIP branch, which predates the changes to
FileHandlerandCsvProcessoron main. The previous head was32f901fif anything from it is still wanted.