Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Get The Value Of Header In CSV

In PowerShell I want to pass the name of a header in a CSV file into another function in the PowerShell script.

How can I retrieve the value-text of a header name into a variable in CSV?

e.g. if I have the following CSV data:

ID Name     Country
-- ----     -------
1  John     United States
2  Beatrice Germany
3  Jouni    Finland
4  Marcel   France

In the above example how can I retrieve the Country column value text as "Country" text into a variable in my script?

(Note: I am familiar with the notation $_.Country to retrieve the value of, for example, "Germany" from a row by importing the CSV in Powershell)

My specific issue is that currently I have the following function in my script:

function GetItemIdFromTitle([string]$LookupTitle, [ref]$LookupId)
{   
    $LookupField = $LookupList.Fields["DEPTCATEGORY"]   
    $LookupItem = $LookupList.Items | where {$_['DEPTCATEGORY'] -like "*$LookupTitle*"} 
    $LookupId.Value = $LookupItem.ID
}

This currently takes a string value -> $LookupTitle and uses that to find an item in a SharePoint list. As you can see in the script I am hard-coding in the column name as "DEPTCATEGORY". This is the column name that will be looked up to in the SharePoint list.

Instead of hard-coding the column name I want to pass in the name of the column for the corresponding $LookupTitle value and replace the hard-coded "DEPTCATEGORY".

I am calling the above function as follows:

#GET THE LOOKUP COLUMN ID       
GetItemIdFromTitle $_.DEPTCAT ([ref]$LookupIdValue)

( $_.DEPTCAT is the value from the row in the CSV column. )

Can I do something like

$myCountryColumnName = $_.Country.Property.Title 

or

$myCategoryColumnName = $_.DEPTCAT.Property.Name

to get the column name from the CSV?

like image 764
motionpotion Avatar asked Sep 10 '14 11:09

motionpotion


People also ask

What is header in CSV?

A header of the CSV file is an array of values assigned to each of the columns. It acts as a row header for the data. Initially, the CSV file is converted to a data frame and then a header is added to the data frame. The contents of the data frame are again stored back into the CSV file.

Should CSV files have headers?

From those guidelines and giving the lack of standardization, the header line is optional in a CSV file. When present, the header line must be the first line in the file and must contain the same number of fields as the records. Header and records lines must use the same field delimiters.

Can a CSV file have multiple headers?

Step 2: Read CSV file with multiple headersTo read the above CSV file which has two headers we can use read_csv with a combination of parameter header . The parameter is described as: Row number(s) to use as the column names, and the start of the data.


1 Answers

If you have an object in $obj, you could list all the property headers like this:

$obj | Get-member -MemberType 'NoteProperty' | Select-Object -ExpandProperty 'Name'

This is an array, so you can reference them individually like this:

($obj | Get-member -MemberType 'NoteProperty' | Select-Object -ExpandProperty 'Name')[0]

This would just give you the name of the first property for instance.

like image 80
ojk Avatar answered Sep 23 '22 00:09

ojk