[英]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.