<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div>Justin,</div><div>Funny thing... I was reading and writing csv files just now. &nbsp;</div><div>I preprocess input files to make sure all the lines have same number of fields as the header</div><div><br></div><div>checkcsv.py &lt; input.csv > good.csv 2>bad.csv</div><div><br></div><div><div>The good lines go to stdout, the bad lines go to stderror.</div><div><br></div><div><br></div></div><div>#!/usr/bin/env python</div><div>import csv</div><div>import sys</div><div><br></div><div>reader = csv.reader(sys.stdin)</div><div>error = csv.writer(sys.stderr)</div><div>output = csv.writer(sys.stdout)</div><div><br></div><div>header =&nbsp;reader.next()</div><div>fields = len(header)</div><div><br></div><div>output.writerow(header)</div><div><div><br></div></div><div>for line in reader:</div><div>&nbsp;&nbsp; &nbsp;if len(line) == fields:</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;output.writerow(line)</div><div>&nbsp;&nbsp; &nbsp;else:</div><div>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;error.writerow(line)</div><div><br></div><div>Jason</div><div><br></div><div>On Apr 20, 2008, at 5:24 PM, Tabatchnick, Justin wrote:</div><div><br class="Apple-interchange-newline"><blockquote type="cite"> <div> <div><font face="Arial" size="2"><span class="010252200-21042008">Hi </span></font></div> <div><font face="Arial" size="2"><span class="010252200-21042008"></span></font>&nbsp;</div> <div><font face="Arial" size="2"><span class="010252200-21042008">I am running an imported file and want to redirect the output to a csv file , what is the best way to accomplish this ?</span></font></div> <div><font face="Arial" size="2"><span class="010252200-21042008"></span></font>&nbsp;</div> <div><font face="Arial" size="2"><span class="010252200-21042008">Thanks</span></font></div> <div><font face="Arial" size="2"><span class="010252200-21042008"></span></font>&nbsp;</div> <div><font face="Arial" size="2"><span class="010252200-21042008">Justin Tabatchnick</span></font></div> <div><font face="Arial" size="2"><span class="010252200-21042008">Intel, Folsom</span></font></div></div> _______________________________________________<br>Baypiggies mailing list<br><a href="mailto:Baypiggies@python.org">Baypiggies@python.org</a><br>To change your subscription options or unsubscribe:<br>http://mail.python.org/mailman/listinfo/baypiggies</blockquote></div><br></body></html>