Solved

Is it possible to read a CSV if the headers have spaces between them ?

Posted on 2013-01-15
5
335 Views
Last Modified: 2013-01-15
CSV contains the following headers:
Last Name,First Name,Dept Descrip,Social Security Numbers,Employment Category,Job Title,Work Phone,Room,Clock Number,Date Of Birth,Div Descrip

Open in new window

Or I have to manually change it to be like:
LastName,FirstName,DeptDescrip,SocialSecurityNumbers,EmploymentCategory,JobTitle,WorkPhone,Room,ClockNumber,DateOfBirth,DivDescrip

Open in new window

Thanks,
0
Comment
Question by:namerg
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 11

Accepted Solution

by:
apathy42 earned 250 total points
ID: 38779014
Yes, you can.  To access the items, you would need to do something like this:

$data = Import-CSV filename.csv

foreach ($line in $data) {
 $lastname = $data."Last Name"
 $firstname = $data."First Name"
}

...and so on.  I tested it, this works for me.
0
 
LVL 70

Assisted Solution

by:Qlemo
Qlemo earned 250 total points
ID: 38779016
Since the data is comma separated, spaces will not harm. But you'll have to use quotes whenever you refer to properties containing special chars:
@"
Name One,Name Two
one, two
"@ | ConvertFrom-Csv | % { $_.'Name One' }

Open in new window

or
@"
Name One,Name Two
one, two
"@ | ConvertFrom-Csv | % { $_.{Name One} }

Open in new window

0
 

Author Comment

by:namerg
ID: 38779030
And what if I want to display it through a write-output like

write-output "Last name: $($file2_user.LastName)"

Thanks,
PS: Never mind, I got it.
0
 

Author Comment

by:namerg
ID: 38779055
@apathy42: Thank you very much.
@Qlemo: You are way too advanced for me :) Your shortcuts in the code hard to get for me :( sorry.
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 38779324
@" and "@  build a multi-line string, and the combination with convertfrom-csv has been used here to build a closed example only.
% is the same as foreach-object.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question