How to extract a single row from multiple CSV files to a new file
<p>I have hundreds of CSV files on my disk, and one file added daily and I want to extract one row from each of them and put them in a new file. Then I want to daily add values to that same file. CSV files looks like this:</p>

<pre><code>business_day,commodity,total,delivery,total_lots
.
.
20160831,CTC,,201710,10
20160831,CTC,,201711,10
20160831,CTC,,201712,10
20160831,CTC,Total,,385
20160831,HTC,,201701,30
20160831,HTC,,201702,30
.
.
</code></pre>

<p>I want to fetch the row that contains 'Total' from each file. The new file should look like:</p>

<pre><code>business_day,commodity,total,total_lots
20160831,CTC,Total,385
20160901,CTC,Total,555
.
.
</code></pre>

<p>The raw files on my disk are named '20160831_foo.CSV', '20160901_foo.CSV etc..</p>

<p>After Googling this I have yet not seen any examples on how to extract only one value from a CSV file. Any hints/help much appreciated. Happy to use pandas if that makes life easier. </p>

<p>Read the csv files and process them directly like so: </p>

<pre><code>with open('some.csv', newline='') as f:
  reader = csv.reader(f)
  for row in reader:
    # do something here with `row`
    break
</code></pre>

<p>I would recommend appending rows onto a list after processing for the rows that you desire, and then passing it onto a <a href="http://pandas.pydata.org/" rel="nofollow">pandas Dataframe</a> that will simplify your data manipulations a lot.</p>

python python-3.x csv pandas