【发布时间】:2021-02-11 11:08:57
【问题描述】:
我有一个如下所示的文件:
#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
我正在尝试编写一个代码,如果它包含以下任何字符,则它会在 file1.txt 中获取 row[0]:
1. Any numeric character (digit).
2. Any character from the alphabet.
3. An asterisk (*) or a plus (+) sign.
我的代码现在没有给出正确的输出,因为我不知道如何添加需求 2 和 3。
这是我的代码:
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)
这是我期望的输出:
A11 2 x x
*12 2 x x
LOK 3 x x
**r1 1 1 2
+12 3 x x
【问题讨论】:
标签: python csv for-loop filter