Solved

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

Posted on 2013-01-15
5
331 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
  • 2
  • 2
5 Comments
 
LVL 11

Accepted Solution

by:
apathy42 earned 250 total points
Comment Utility
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 68

Assisted Solution

by:Qlemo
Qlemo earned 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
@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 68

Expert Comment

by:Qlemo
Comment Utility
@" 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Hi all.   The other day I had to change the passwords for a bunch of users on the fly. Because they were so many, I decided to do it in an automated way and I would like to share it with you all.   If you are not doing it directly in a Domain Co…
How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now