简体   繁体   English

如何根据特殊字符过滤 csv 文件?

[英]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:我正在尝试编写一个在file1.txt中抓取row[0]的代码,如果它包含以下任何字符:

 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.我的代码现在没有给出正确的 output 因为我不知道如何添加要求 2 和 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:这是我期望的 output:

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

Assuming you are using Python3, Always specify an encoding while opening a file eg 'utf-8' and newline='' Also CSV dialect can help,假设您使用的是 Python3,请始终在打开文件时指定编码,例如 'utf-8' 和 newline='' CSV 方言也可以提供帮助,

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何根据字符串和特殊字符过滤 pd.Dataframe? - How to filter pd.Dataframe based on strings and special characters? 如何使用 Pyspark 读取日志文件并根据控制字符进行过滤? - How to read log files and filter based on control characters using Pyspark? 过滤特殊字符,对它们进行计数,然后在另一个csv上重写 - Filter special characters, count them, and rewrite on another csv 如何在python中的csv中替换特殊字符列表 - How to replace a list of special characters in a csv in python 写入和读取csv文件时出现特殊字符(\\ r)的问题 - Problems with special characters (\r) when writing and reading csv files 过滤 dataframe 中的特殊字符 - filter special characters in a dataframe 过滤一些特殊字符 - Filter some special characters 如何在python中读取带有特殊字符的文件 - how to read files with special characters in python 如何使用 Pandas 读取文件(带有特殊字符)? - How to read files (with special characters) with Pandas? 如何在 Python 中读取 csv 文件(带有特殊字符)? 如何解码文本数据? 从文件中读取编码文本并转换为字符串 - How to read csv files (with special characters) in Python? How can I decode the text data? Read encoded text from file and convert to string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM