0

Hi I want to filter out rows whose 2nd column (Hex) has value 80 or 81 and filter out rows whose 2nd column is 0 only if its below the row which has 81 in the second column:

sample.csv :

Delta,Hex,Type,Time

0,81,DTE,10 pm
1,0,DTE,10 pm
0,01,DTE,10 pm
1,1c,DTE,9 pm
1,0,DTE,10 pm
0,1d,DTE,10 pm
1,0,DTE,10 pm

So here's the script which I wrote which filters out rows with values 80 81 and 0 in the second column(Hex).

Script.py:

import csv

with open('sample.csv', 'r') as f:

  rows=csv.DictReader(f, fieldnames=None,restkey=None,restval=None,dialect='excel',delimiter=",")

  filtered_rows=filter(lambda p:'80' != p['Hex'] and '81' != p['Hex'] and '0' != p['Hex'], rows)

  for i in filtered_rows :

    print (i['Hex'])

So it filters out all the rows which has 0 in their second column.

Current output with the above script:

01
1c
1d

But I dont want to filter the row which has 0 in the second column if its not below the row which has 81 in the second column:

Expected output :

01
1c
0
1d
0

1 Answer 1

1

If you need a non-current state, the 0 condition, you should filter the list iterating it, not with lambda

>>> import csv
>>> with open('sample.csv', 'r') as f:
...     rows=csv.DictReader(f, fieldnames=None,restkey=None,restval=None,dialect='excel',delimiter=",")
...     filtered_rows=[]
...     previous_flag=False
...     for item in rows:
...         if item['Hex'] not in ['81','80'] and not (previous_flag and  item['Hex'] == '0'):
...             filtered_rows.append(item)
...         previous_flag = item['Hex'] == '81'
...     for i in filtered_rows :
...         print (i['Hex'])
... 
01
1c
0
1d
0
Sign up to request clarification or add additional context in comments.

2 Comments

This script will work only if 81 occurs every time after 0 but that's not the case. If 6th row is 0,1d,DTE,10 pm it will not append the 5th row which it should.
could you edit your question with new data and the desired result?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.