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>From the <code>csv.reader</code> docstring:</p>

<blockquote>
  <p><em>csvfile</em> can be any object which supports the iterator protocol and returns a string each time its <code>__next__()</code> method is called</p>
</blockquote>

<p>You can feed <code>csv.reader</code> with a generator iterator that yields only the selected rows.</p>

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

python csv