Filter rows in a CSV file and then sort them based on a column
<p>Trying to parse the data file (below) to find only the rows where the user started before a certain date. then order the values from the words column from these rows in ascending order (by start date)</p>

<pre class="lang-none prettyprint-override"><code>id, name, start_date, role, end_date, words
657, mystical, 1351140260, cleaner, 1951140260, very lazy
1987, kanyau, 1451189768, watchman, 1539742445, sleeping
</code></pre>

<p>Can anyone assist?</p>

<p>P.S: newbie here but here is what I have been playing around with.</p>

<pre><code>date_pivot = "6/09/2010 00:00:00"
d = datetime.strptime(date_pivot, "%d/%m/%Y %H:%M:%S")
date_pivot = time.mktime(d.timetuple())
dp = int(date_pivot)
infile = csv.DictReader(open("sample_data.csv","rb"), delimiter=",")
previous_users = [row for row in infile if row['start_date'] &lt; 'dp']
#print previous_users
with open('final_test.csv','wb') as fou:
    dw = csv.DictWriter(fou, previous_users.keys())
    dw.writeheader()
    dw.writerow(my_dict)
</code></pre>

<p>Should be fairly simple. Since you need to type convert and do lookup for your key function, a <code>lambda</code> is simplest:</p>

<pre><code>previous_users.sort(key=lambda row: int(row['start_date']))
</code></pre>

<p>A note: Passing <code>previous_users.keys()</code> to <code>DictWriter</code> as the fieldnames is doubly wrong. One, you'd need to do <code>previous_users[0].keys()</code> (after verifying it was non-empty), because <code>previous_users</code> is a <code>list</code> of <code>dict</code>, not <code>dict</code>. Two, <code>dict</code>s have no defined ordering, so your output columns will likely be rearranged. If that's not a problem, so be it. But you probably want to explicitly pass the field names in the desired order or read them in the right order from the <code>DictReader</code>, e.g. <code>csv.DictWriter(fou, infile.fieldnames)</code></p>

<p>Additional typo note: Presumably you want to compare <code>int(row['start_date']) &lt; dp</code>; you need to convert to <code>int</code>, and you want to compare to the value in <code>dp</code>, not the string <code>"dp"</code>.</p>

python csv