[Tutor] ignoring certain lines while reading through CSV

Joel Goldstick joel.goldstick at gmail.com
Sat Jan 28 00:04:15 CET 2012


On Fri, Jan 27, 2012 at 5:48 PM, Abhishek Pratap <abhishek.vit at gmail.com> wrote:
> Hi Joel
>
> Here is a sample
>
> ['1', 'AAAAAAA', '4344', '0.001505'] : want to keep this one
>
> ['#', 'AAAAAAA', '4344', '0.001505'] : and throw this one

Ok, so you are getting single quotes around your data.  So do
row[0].startswith("#") to test your row.
You may be able to test for row[0]=="#" if you always get only the #
in the first position of the row.
>
>
> You are right I am checking after parsing. I dint find an option in
> csv.reader to ignore lines.
>
> -Abhi
>
>
>
>
>
> On Fri, Jan 27, 2012 at 2:42 PM, Joel Goldstick
> <joel.goldstick at gmail.com> wrote:
>> On Fri, Jan 27, 2012 at 5:13 PM, Abhishek Pratap <abhishek.vit at gmail.com> wrote:
>>> Hi Guys
>>>
>>> I am wondering if there is a keyword to ignore certain lines ( for eg
>>> lines starting with # ) when I am reading them through stl module csv.
>>>
>>> Example code:
>>>
>>> input_file = sys.argv[1]
>>> csv.register_dialect('multiplex_info',delimiter=' ')
>>>
>>> with open(input_file, 'rb') as fh:
>>>    reader= csv.reader(fh,'multiplex_info')
>>>    for row in reader:
>>>        print row
>>>
>>>
>>> best,
>>> -Abhi
>>> _______________________________________________
>>> Tutor maillist  -  Tutor at python.org
>>> To unsubscribe or change subscription options:
>>> http://mail.python.org/mailman/listinfo/tutor
>>
>> You could look up the docs for csv.reader, but if there isn't, in your
>> for loop you can use row[0].startswith('"#")  to check if your line
>> starts with #.
>> Can you show what the row looks like?
>>
-- 
Joel Goldstick


More information about the Tutor mailing list