Selectively feeding python csv class with lines
<p>I have a csv file with a few patterns. I only want to selectively load lines into the csv reader class of python. Currently, csv only takes a file object. Is there a way to get around this?<br>
In other words, what I need is:</p>

<pre><code>with open('filename') as f:
    for line in f:
        if condition(line):
            record = csv.reader(line)
</code></pre>

<p>But, currently, csv class fails if it is given a line instead of a file object.</p>

<p>Found a solution: As csv expects object which supports  __next__(), I'm using a <strong>StringIO</strong> class to convert string to StringIO object which in turn handles  __next__() and returns one line everytime for csv reader class.</p>

<pre><code>with open('filename') as f:
for line in f:
    if condition(line):
        record = csv.reader(StringIO.StringIO(line))
</code></pre>

python csv