[Baypiggies] Want to redirect output to a csv file

Shannon -jj Behrens jjinux at gmail.com
Tue Apr 22 12:52:34 CEST 2008


Ha!  I wrote very much that same script this week!

$ ./blueplate/parsing/cleancuttsv.py -h
Usage: cleancuttsv.py [options]

Options:
  -h, --help            show this help message and exit
  --assert-head=FIELD1\tFIELD2\t...
                        assert that the first line of the file matches this
  --delete-head         delete the first line of input
  -n NUM, --num-fields=NUM
                        assert that there are this many fields per line

Small world.

-jj

On Sun, Apr 20, 2008 at 9:09 PM, Jason Culverhouse
<jason at mischievous.org> wrote:
>
> Justin,
> Funny thing... I was reading and writing csv files just now.
> I preprocess input files to make sure all the lines have same number of
> fields as the header
>
> checkcsv.py < input.csv > good.csv 2>bad.csv
>
>
> The good lines go to stdout, the bad lines go to stderror.
>
>
> #!/usr/bin/env python
> import csv
> import sys
>
> reader = csv.reader(sys.stdin)
> error = csv.writer(sys.stderr)
> output = csv.writer(sys.stdout)
>
> header = reader.next()
> fields = len(header)
>
> output.writerow(header)
>
> for line in reader:
>     if len(line) == fields:
>         output.writerow(line)
>     else:
>         error.writerow(line)
>
> Jason
>
>
> On Apr 20, 2008, at 5:24 PM, Tabatchnick, Justin wrote:
>
>
>
>
>
> Hi
>
> I am running an imported file and want to redirect the output to a csv file
> , what is the best way to accomplish this ?
>
> Thanks
>
> Justin Tabatchnick
> Intel, Folsom
>  _______________________________________________
> Baypiggies mailing list
> Baypiggies at python.org
> To change your subscription options or unsubscribe:
> http://mail.python.org/mailman/listinfo/baypiggies
>
> _______________________________________________
>  Baypiggies mailing list
>  Baypiggies at python.org
>  To change your subscription options or unsubscribe:
>  http://mail.python.org/mailman/listinfo/baypiggies
>



-- 
I, for one, welcome our new Facebook overlords!
http://jjinux.blogspot.com/


More information about the Baypiggies mailing list