'Iterate through a CSV and select row based on condition, and then select the row before it
SOLVED! Proper code in bottom half.
--
I included a link to a sample from a long CSV file, with any identifying data changed. I need every row that begins with "W", and then every row before it as well. The code I included writes every "W" row to a list. The final line, of course, doesn't work. I would like every previous row from a "W" row to be in its own list. Ultimately, I will combine them into an 8-column csv (using the zip function?), since each of these are 2-row associated data.
(To clarify - the associated rows in the whole table are sometimes in sets of 2, and sometimes in sets of 3. So I can't approach it by counting rows. I don't care about the 3rd row, when it exists. The key is the "W" rows)
What am I not figuring out? I've been searching all day and am not nailing this.
import csv
rows1 = [] #all 'W' rows
rows2 = [] #all rows before 'W' rows
with open ('Businesses.csv', 'r') as file1:
csvreader = csv.reader(file1)
for row in csvreader:
previousrow = row
if row[0].startswith('W'):
rows1.append(row)
rows2.append(previousrow)
#FIGURED IT OUT! With this -
import csv
rows1 = [] #all 'W' rows
rows2 = [] #all rows before 'W' rows
with open ('Businesses.csv', 'r') as file1:
csvreader = csv.reader(file1)
templist = []
for row in csvreader:
if not row[0].startswith('W'):
templist.append(row)
if row[0].startswith('W'):
rows1.append(row)
rows2.append(templist[-1])
Solution 1:[1]
On the last line, row - 1 is taking an array and subtracting 1 from it, which isn't meaningful.
What will work is if you store the current row temporarily (as previousrow for example), then when you check the next current row, if it matches, then save both the current row and the previous row to your accumulator arrays.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Eugenious |
