TangerCity
TangerCity

Reputation: 855

How to filter a csv files based on special characters?

I have a file which looks like this:

#test data
10  x   x   x
A11 2   x   x
*12 2   x   x
LOK 3   x   x
**r1    1   1   2
+Y6 x   x   x
13  x   x   x
+12 3   x   x

I'am trying to write a code which is grabbing row[0] in file1.txt if it contains any of the following characters:

 1. Any numeric character (digit).
 2. Any character from the alphabet.
 3. An asterisk (*) or a plus (+) sign.

My code right now is not giving the correct output because I don't know how to add requirements 2 and 3.

This is my code:

import csv
old_path = 'file_1.txt'

def filter_row(row):
    if len(row) > 1 and row[1].isdigit():
        condition_1 = row[0].isdigit()

        return condition_1

with open(old_path, 'r') as f1:
    reader_f1 = csv.reader(f1, delimiter='\t')

    for row in reader_f1:
        if filter_row(row):
            print(row)

This is the output I expect:

   A11  2   x   x
    *12 2   x   x
    LOK 3   x   x
    **r1    1   1   2
    +12 3   x   x

Upvotes: 0

Views: 458

Answers (1)

Sachin
Sachin

Reputation: 219

Assuming you are using Python3, Always specify an encoding while opening a file e.g. 'utf-8' and newline='' Also CSV dialect can help,

import csv


def check_aphabet(data):
    for ch in data:
        if((ch >= 'a' and ch <= 'z') or (ch >= 'A' and ch <= 'Z')):
            return True
    return False
def filter_row(row):
    data = row[0]
    if all([i.isdigit() for i in data]):
        return False
    elif check_aphabet(data) or all([i.isdigit() for i in data]):
        return True
    elif '*' in data or '+' in data:
        return True
    return  False

with open('delete.csv', encoding='UTF-8', newline='') as csv_file:
    csv_reader = csv.reader(csv_file, delimiter=',')
    for row in csv_reader:
        if filter_row(row):
            print(row)

Upvotes: 1

Related Questions