Windows Vista Forums

Import csv wihout headers

  1. #1


    Altraf Guest

    Import csv wihout headers

    Hi All

    How can I use import-csv to import a csv file that does not contain headers?

    Thanks.

    Altraf



      My System SpecsSystem Spec

  2. #2


    Brandon Shell [MVP] Guest

    Re: Import csv wihout headers

    I would just add them, but if it does not have headers it will take the values
    of the first row, probably not what your after.

    You could do it your self.

    Get-Content file | %{$_.Split(",")}

    Brandon Shell
    ---------------
    Blog: http://www.bsonposh.com/
    PSH Scripts Project: www.codeplex.com/psobject

    A> Hi All
    A>
    A> How can I use import-csv to import a csv file that does not contain
    A> headers?
    A>
    A> Thanks.
    A>
    A> Altraf
    A>



      My System SpecsSystem Spec

  3. #3


    Altraf Guest

    Re: Import csv wihout headers

    Hi Brandon

    Thanks for your response. I had come to a similar conclusion, but was being
    lazy and wondering if it was possible to specify header names. I notice that
    import-csv returns an array with named columns. Using your method how would
    I initialise an array with the header names, but without specifying values?

    Thanks.

    Altraf

    "Brandon Shell [MVP]" wrote:

    > I would just add them, but if it does not have headers it will take the values
    > of the first row, probably not what your after.
    >
    > You could do it your self.
    >
    > Get-Content file | %{$_.Split(",")}
    >
    > Brandon Shell
    > ---------------
    > Blog: http://www.bsonposh.com/
    > PSH Scripts Project: www.codeplex.com/psobject
    >
    > A> Hi All
    > A>
    > A> How can I use import-csv to import a csv file that does not contain
    > A> headers?
    > A>
    > A> Thanks.
    > A>
    > A> Altraf
    > A>
    >
    >
    >

      My System SpecsSystem Spec

  4. #4


    Brandon Shell [MVP] Guest

    Re: Import csv wihout headers

    You could just create your own custom object and fill it in. You could also
    use select-object to create it for you.

    $myobj = "" | select name1,name2,name3....

    or

    gc file | %{$_.Split(",")} | select-object @{n="name1";e={$_[0]},
    @{n="name2";e={$_[1]},
    @{n="name3";e={$_[2]},
    @{n="name4";e={$_[3]},
    .....

    Brandon Shell
    ---------------
    Blog: http://www.bsonposh.com/
    PSH Scripts Project: www.codeplex.com/psobject

    A> Hi Brandon
    A>
    A> Thanks for your response. I had come to a similar conclusion, but
    A> was being lazy and wondering if it was possible to specify header
    A> names. I notice that import-csv returns an array with named columns.
    A> Using your method how would I initialise an array with the header
    A> names, but without specifying values?
    A>
    A> Thanks.
    A>
    A> Altraf
    A>
    A> "Brandon Shell [MVP]" wrote:
    A>

    >> I would just add them, but if it does not have headers it will take
    >> the values of the first row, probably not what your after.
    >>
    >> You could do it your self.
    >>
    >> Get-Content file | %{$_.Split(",")}
    >>
    >> Brandon Shell
    >> ---------------
    >> Blog: http://www.bsonposh.com/
    >> PSH Scripts Project: www.codeplex.com/psobject
    >> A> Hi All
    >> A>
    >> A> How can I use import-csv to import a csv file that does not
    >> contain
    >> A> headers?
    >> A>
    >> A> Thanks.
    >> A>
    >> A> Altraf
    >> A>


      My System SpecsSystem Spec

  5. #5


    Kiron Guest

    Re: Import csv wihout headers

    For a file with data that contains commas in the values:
    @'
    Canada,"33,321,200",0.5
    USA,"304,627,000",4.57
    '@ > .\file.csv

    # no headers
    ipcsv .\file.csv

    # modify the file, remember Unicode is the default encoding
    sc .\file.csv 'Country, Population, Percent', (gc x\file.csv)

    # with headers
    ipcsv .\file.csv

    # v2 CTP's Import-Csv has a -Header parameter
    @'
    Canada,"33,321,200",0.5
    USA,"304,627,000",4.57
    '@ > .\file.csv

    ipcsv .\file.csv -h Country, Population, Percent

    --
    Kiron

      My System SpecsSystem Spec

  6. #6


    Kiron Guest

    Re: Import csv wihout headers

    # sorry, typo:
    sc .\file.csv 'Country, Population, Percent', (gc x\file.csv)

    # should be:
    sc .\file.csv 'Country, Population, Percent', (gc .\file.csv)

    --
    Kiron

      My System SpecsSystem Spec

  7. #7


    Altraf Guest

    Re: Import csv wihout headers

    Hi Kiron

    Thanks for your response. This looks good, but I don't want to alter the
    original file as it is a log file created by an application (Jungle Disk - I
    want to email myself a summary of the daily log). I suppose I could create a
    temporary file and delete it afterwards. Of course I could install the v2
    CTP, but that looks to easy and the suggestions received so far are good
    learning.

    Thanks.

    Altraf

    "Kiron" wrote:

    > For a file with data that contains commas in the values:
    > @'
    > Canada,"33,321,200",0.5
    > USA,"304,627,000",4.57
    > '@ > .\file.csv
    >
    > # no headers
    > ipcsv .\file.csv
    >
    > # modify the file, remember Unicode is the default encoding
    > sc .\file.csv 'Country, Population, Percent', (gc x\file.csv)
    >
    > # with headers
    > ipcsv .\file.csv
    >
    > # v2 CTP's Import-Csv has a -Header parameter
    > @'
    > Canada,"33,321,200",0.5
    > USA,"304,627,000",4.57
    > '@ > .\file.csv
    >
    > ipcsv .\file.csv -h Country, Population, Percent
    >
    > --
    > Kiron
    >

      My System SpecsSystem Spec

  8. #8


    Kiron Guest

    Re: Import csv wihout headers

    Sure Altraf, here is another suggestion, note the use of [Regex]'s Split Method and Out-String at the end of the pipeline, this ensures that $body will output the formatted results in your Emal as [String]. If you omit Out-String you'll get weird output, because $body --not a [String]-- will be cast as string before sending the Email:

    @'
    Canada,"33,321,200",0.5
    USA,"304,627,000",4.57
    '@ > .\file.csv

    $body = gc .\file.csv | % {
    $val = [regex]::split($_,'(?<="),|,(?=")')
    $obj = new-object psObject
    $obj | add-member noteProperty Country $val[0] -p |
    add-member noteProperty Population $val[1] -p |
    add-member noteProperty Percent $val[2] -p
    } | ft -a | out-string

    $body.getType().name

    $body

    --
    Kiron

      My System SpecsSystem Spec

  9. #9


    Altraf Guest

    Re: Import csv wihout headers

    Hi Kiron

    Thanks, for another solution. Typical Powershell - lots of options for
    achieving your goal.

    Thanks.

    Altraf

    "Kiron" wrote:

    > Sure Altraf, here is another suggestion, note the use of [Regex]'s Split
    > Method and Out-String at the end of the pipeline, this ensures that $body
    > will output the formatted results in your Emal as [String]. If you omit
    > Out-String you'll get weird output, because $body --not a [String]-- will
    > be cast as string before sending the Email:
    >
    > @'
    > Canada,"33,321,200",0.5
    > USA,"304,627,000",4.57
    > '@ > .\file.csv
    >
    > $body = gc .\file.csv | % {
    > $val = [regex]::split($_,'(?<="),|,(?=")')
    > $obj = new-object psObject
    > $obj | add-member noteProperty Country $val[0] -p |
    > add-member noteProperty Population $val[1] -p |
    > add-member noteProperty Percent $val[2] -p
    > } | ft -a | out-string
    >
    > $body.getType().name
    >
    > $body
    >
    > --
    > Kiron
    >

      My System SpecsSystem Spec

Import csv wihout headers

Similar Threads
Thread Thread Starter Forum Replies Last Post
only headers Irwin Opper Live Mail 2 05 Apr 2010
Headers Only Michael Dobony Live Mail 19 05 Apr 2009
CSV headers PSApple PowerShell 5 14 Dec 2007
Headers Fritz Vista mail 2 26 Nov 2007
I need driver for WebCam Genius Look 300K Vista wihout virus troja Fernando Arias Vista hardware & devices 1 19 Oct 2007