-
Notifications
You must be signed in to change notification settings - Fork 13
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
Field with any newline character is truncated #95
Comments
The current implementation of the csv formatter was not intended to support fields with embedded line breaks; however, I would not be opposed to a pull request to allow for this capability, provided that it included at least one test. |
From previous experience with csv in php: This would do the trick, for comma as delimiter - but obviously you want to support different values for the parameters. protected function csvEscape($data, $delimiter = ',', $enclosure = '"', $escapeChar = "\\")
{
return implode(',', array_map(static function (string $value): string {
if (!preg_match('@["\n,]@', $value)) {
return $value;
}
return '"' . str_replace('"', '""', $value) . '"';
}, $data)) . "\n";
} EDIT: A |
The definition of the csv data type in the output formatters project is that each record appears on a separate line; therefore, if embedded newlines are to be supported by this format, they would need to be converted to some form of escape character. Your program could install a custom formatter, or replace the csv formatter with a custom implementation that behaves the way that you want, but we can't add embedded newlines to the csv formatter provided here. |
May I ask why this definition exists in this way and is such a hard requirement? If BC is a concern, we can look for ways around that.
This would not be understood by typical programs that can read csv, e.g. spreadsheet applications. |
Yes, it would require a major version to change the behavior. As for ways around it, perhaps a property could stipulate behavior, but it's already possible to install your own formatter, or replace a standard one if you'd prefer different behavior in your application. |
Would you accept an alternative csv formatter as part of this package?
https://stackoverflow.com/questions/10140999/csv-with-comma-or-semicolon
(this is in one of the comments) So, not a real standard, but still better than what php produces with native I think the remaining problem with office applications is character encoding, which is independent from the escape char and enclosure syntax. |
Actually, how? For context: Atm I am working with |
Actually, the inconsistent handling in php has been fixed, it seems :) In the docs for all the csv functions we find this:
|
Hello, I was trying to create a CSV file where one of the fields lists multiple URLs. For peoples' viewing pleasure, I thought I will separate those URLs using "\n". Kind of like this:
But the output from
drush insert-command-here --format=csv
kept producing a truncated version like the following:Turned out the CSV formatter is keeping the first line of each CSV row only:
output-formatters/src/Formatters/CsvFormatter.php
Line 127 in fb12960
Fix is easy (patch pasted below). I am happy to raise a pull request, but I am wondering if the current behaviour is intentional or not. Please advice.
The text was updated successfully, but these errors were encountered: