Home Dashboard Directory Help
Search

Import-Csv and blank lines by Shay Levi


Status: 

Closed
 as Fixed Help for as Fixed


38
5
Sign in
to vote
Type: Suggestion
ID: 767851
Opened: 10/17/2012 2:06:49 PM
Access Restriction: Public
4
Workaround(s)
view

Description

Consider the following CSV file:

# test.csv
Name,Id
foo,1



# it ends here



When imported, blank lines are not ignored and converted to blank objects

PS> Import-Csv .\test.csv | fl *


Name : foo
Id : 1

Name :
Id :

Name :
Id :


Blank lines can be easily introduced when authoring csv content, sometimes they are ignored or not spotted before the content is saved and it can lead to unexpected results,
I can't see the value of empty objects, not to mention filtering them out.

It would be great if Import-Csv (ConvertFrom-Csv) ignored blank lines or at least had a switch parameter (-IgnoreEmptyLines) to do that
Details
Sign in to post a comment.
Sign in to post a workaround.
Posted by sunan ketyoi on 7/14/2014 at 9:10 AM
workarounts(2)










































































Posted by sunan ketyoi on 7/14/2014 at 9:06 AM
workarounts(2)
Posted by Matthew BETTON on 9/10/2013 at 5:53 AM
Be aware about this sample :

# test.csv
Name,Id
foo,1
,
,
,
# it ends here

So values of properties are not null but empty.

A solution for this :

#v2
Import-Csv .\test.csv | Where-Object {
    ($_.PSObject.Properties | Where-Object {$_.Value})
}


Posted by Shay Levi on 11/2/2012 at 9:55 AM
#v2
Import-Csv .\test.csv | Where-Object {
    ($_.PSObject.Properties | Foreach-Object {$_.Value}) -ne $null
}

#v3
Import-Csv .\test.csv | Where-Object {
    $_.PSObject.Properties.Value -ne $null
}