Opticgenius Opticgenius - 7 months ago 11
Python Question

keeping track of valid rows in csv file

I have a csv file like this:

1 2 3 4 5 6
4 5 6 3 # 2
2 4 5 6 3 1
3 4 % $ 2 1
1 2 3 4 5 8


I only want the rows that have all numbers, and not any other characters. Is there a way to keep track of only the correct rows, which only have numbers in them.

So far I only have this:

import csv
def get_rows(csv_file):

csv_read = open(csv_file)
reader = csv.reader(csv_read)

invalid_rows = 0
for row in reader:


I'm just not sure how to keep track of the correct rows, and be able to add the invalid rows to a counter. I want to be able to use the correct rows in the future, and simply add the number of incorrect rows to a counter.

Answer

Here is a pure python solution:

$ cat data 
1 2 3 4 5 6
4 5 6 3 # 2
2 4 5 6 3 1 
3 4 % $ 2 1
1 2 3 4 5 8
$ cat p.py 
import csv, re

validRows=[]
with open('data', 'r') as f:
    reader=csv.reader(f, delimiter=' ')
    for row in reader:
        if all([re.match(r'^\d+$', item) is not None for item in row]):
            validRows+=[row]
print(validRows)
$ python3 p.py 
[['1', '2', '3', '4', '5', '6'], ['1', '2', '3', '4', '5', '8']]
$

Without using re:

import csv

validRows=[]
with open('data', 'r') as f:
    reader=csv.reader(f, delimiter=' ')
    for row in reader:
        if all([item.isdigit() for item in row]):
            validRows+=[row]
print(validRows)