|
Re: parsing csv files with fields that may contain commas I would add headers to the CSV
Here is my test file
====== TEST FILE =======
Header1,Header2,Header3
this is field1,"fieldtwo,fieldtwo",this is field 3
====== TEST FILE =======
You can then use import-csv to import for parsing
PS> $csvimporttest = import-csv test.csv
PS> $csvimporttest
Header1 Header2
Header3
------ ------
------
this is field1 fieldtwo,fieldtwo
this is field 3
As you can see... the quotes worked as expected.
Then to parse like this
===== CODE =====
$csvimporttest = import-csv test.csv
foreach($object in $csvimporttest)
{
write-host $object.header1
write-host $object.header2
write-host $object.header3
}
===== CODE =====
"Frank" <Frank@discussions.microsoft.com> wrote in message
news:FED131FD-F83D-4E0B-BE0E-7C314E314855@microsoft.com...
> Hi,
>
> What is the best way to parse a csv comma delimeted file which may have
> commas in the fields. If there are commas in the field, it is enclosed in
> double quotes, ie.
>
> field1,field2,field3
> "field1, abc",field2,field3
>
>
> So, in the second row, the first field would be: "field1, abc"
>
> Thanks in advance,
>
> |