You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Import-Csv does not handle newlines containing just \r (Carriage Return) #3692
It seems that the ImportCsvHelper method located in my C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\Microsoft.PowerShell.Commands.Utility\v4.0_3.0.0.0__31bf3856ad364e35\Microsoft.PowerShell.Commands.Utility.dll is the culprit.
It looks like the code here:
private bool IsNewLine(char ch)
{
bool result = false;
if (ch == '\n')
{
result = true;
}
else if (ch == '\r' && this.PeekNextChar('\n'))
{
result = true;
}
return result;
}
And yes, using \r\n and \n both work fine. Its only just the plain `r that causes this issue. If I could, I would beat whoever creates files with \r as a line ending, but that's illegal and stuff.
The fact that Get-Content can read a file that uses just carriage returns means that there is inconsistency between using Get-Content foo.csv | ConvertFrom-Csv and Import-Csv. I would think those two usages should perform the same.
Now I see what you're referring with the 'r`'. I notice the inconsistency between import-csv and get-content. Yeap! This might be a Bug!!
At the same time PowerShell is not working in WSL Bash Console as some of it previous behavior, like Scroll-Up and screen refresh issues, are back again making it hard to work with it.
An atomic property that sets the line terminators flag to either an array containing the single provided string value, or the provided array. The default is ["\r\n", "\n"].
It should also be noted that the SQL bcp utility has support of custom (EOL) terminators.
Personally, I'm not sure that it is worth making a -LineDelimiter parameter, given that I can Get-Content a csv file, split it using any delimiter I want, and then ConvertFrom-Csv to already accomplish the same thing. As just a regular powershell user, I don't expect import-csv or get-content to have custom EOL characters/terminators/delimiters.
Maximo Trinidad (@MaximoTrinidad) You beat me to it: I meant to point you to the fact that Get-Content already has a -Delimiter parameter, though its current behavior is surprising: #3706
I agree that should be fixed to work the same as get content. I don't think
that we should add anything else though (at least, not based on this ticket)
There are 3 line ending variants supported by the PowerShell parser - CRLF (Windows), LF (Unix), and CR (old Apple), so it's very reasonable for CSV to do the same.
Ilya (@iSazonov) I'm still having an issue with this. My PS version is 5.1.18362.145. When I import a CSV where a line ends in \r instead of \r\n, the final value gets set to \r instead of an empty string, which is what I'd expect. Here's sample code:
Django reporting outputs CSVs that end with \r instead of \r\n, which is causing errors in some scripts I have. Now that I understand the issue, I can work around it, but it seems like the issue isn't fully resolved with Import-Csv.
Bobby Crotty (@bcrotty) did you notice that this repository is for PowerShell Core (with version 7 preview 5 recently release)?
the difference is explained here
Actually I am surprised that an issue for the very different product Windows PowerShell 5.1 (not covered here) wasn't closed right away.
Activity
Szeraax commentedon May 3, 2017
By the way, Get-Content does handle this case correctly. My solution for these files is to use Get-Content foo.csv | ConvertFrom-Csv.
MaximoTrinidad commentedon May 3, 2017
Hum! I normally use both "r
n" then I can get the expected result. I don't think something is wrong here."one,two,three
rnthree,three,three"| out-file test.csvimport-csv test.csv
Results:
one two three
three three three
By the way, just a reminder, for Windows 5.x issues need to routed to UserVoice forum: https://windowsserver.uservoice.com/forums/301869-powershell
Szeraax commentedon May 3, 2017
Good to know about UserVoice!
And yes, using \r\n and \n both work fine. Its only just the plain `r that causes this issue. If I could, I would beat whoever creates files with \r as a line ending, but that's illegal and stuff.
The fact that Get-Content can read a file that uses just carriage returns means that there is inconsistency between using Get-Content foo.csv | ConvertFrom-Csv and Import-Csv. I would think those two usages should perform the same.
MaximoTrinidad commentedon May 4, 2017
Now I see what you're referring with the 'r`'. I notice the inconsistency between import-csv and get-content. Yeap! This might be a Bug!!
At the same time PowerShell is not working in WSL Bash Console as some of it previous behavior, like Scroll-Up and screen refresh issues, are back again making it hard to work with it.
jeffbi commentedon May 4, 2017
Isn't "`r" carriage-return (CR, 0x0D) rather that line-feed (LF, 0x0A)? Unix-style line endings are LF. Old, pre-OS X mac line-endings were CR.
Szeraax commentedon May 4, 2017
thezim commentedon May 4, 2017
-LineDelimiter would be a nice addition.
Szeraax commentedon May 4, 2017
MaximoTrinidad commentedon May 5, 2017
I think it should also be added in the "Get-Content" in order to keep some kind on consistent behavior.
+1 on to add the parameter '-LineDelimiter'.
:)
iSazonov commentedon May 5, 2017
There is the informational RFC 4180 for CSV:
This obviously does not take into account Unix style EOL (LF) that we should support.
I agree that we should support old MAC style (CR, '\r') too.
Modern W3C standard - Model for Tabular Data and Metadata on the Web
line terminators
It should also be noted that the SQL bcp utility has support of custom (EOL) terminators.
Szeraax commentedon May 5, 2017
Personally, I'm not sure that it is worth making a -LineDelimiter parameter, given that I can Get-Content a csv file, split it using any delimiter I want, and then ConvertFrom-Csv to already accomplish the same thing. As just a regular powershell user, I don't expect import-csv or get-content to have custom EOL characters/terminators/delimiters.
I'll let others decide that one though.
mklement0 commentedon May 5, 2017
Maximo Trinidad (@MaximoTrinidad) You beat me to it: I meant to point you to the fact that
Get-Contentalready has a-Delimiterparameter, though its current behavior is surprising: #3706MaximoTrinidad commentedon May 5, 2017
LOL Michael Klement (@mklement0)!!
:)
1 remaining item
Szeraax commentedon May 5, 2017
thezim commentedon May 5, 2017
Szeraax yes, fix it first.
mi-hol commentedon Nov 5, 2017
Steve Lee (@SteveL-MSFT) maybe this issue could be fixed using the code proposed by Szeraax any time soon?
iSazonov commentedon Nov 5, 2017
I can pull PR on next week if the fix will be approved.
SteveL-MSFT commentedon Nov 5, 2017
Seems like treating a single CR the same as a single LF is reasonable.
lzybkr commentedon Nov 6, 2017
There are 3 line ending variants supported by the PowerShell parser - CRLF (Windows), LF (Unix), and CR (old Apple), so it's very reasonable for CSV to do the same.
See here for more excessive details.
bcrotty commentedon Nov 4, 2019
Ilya (@iSazonov) I'm still having an issue with this. My PS version is 5.1.18362.145. When I import a CSV where a line ends in
\rinstead of\r\n, the final value gets set to\rinstead of an empty string, which is what I'd expect. Here's sample code:Django reporting outputs CSVs that end with
\rinstead of\r\n, which is causing errors in some scripts I have. Now that I understand the issue, I can work around it, but it seems like the issue isn't fully resolved withImport-Csv.mi-hol commentedon Nov 4, 2019
Bobby Crotty (@bcrotty) did you notice that this repository is for PowerShell Core (with version 7 preview 5 recently release)?
the difference is explained here
Actually I am surprised that an issue for the very different product Windows PowerShell 5.1 (not covered here) wasn't closed right away.
bcrotty commentedon Nov 4, 2019
mi-hol Got it.
iSazonov commentedon Nov 5, 2019
Bobby Crotty (@bcrotty) You can use Windows 10 Feedback tool or UserVoice site to report but before check latest Windows 10 builds.