我有一个.csv文件,里面有这样的数据:
index, name, id
1 john 512
2 Anne 895
3 Angel 897
4 Lusia 777
所以我想通过名字词尾来过滤它们,只得到有元音词尾的名字。结果一定是这样的:
index, name, id
1 Anne 895
2 Lusia 777
过滤后,我想将结果保存在另一个.csv文件中。我正在尝试各种方法来获得正确的结果,但是,我做不到。请帮帮我:(
提问前尝试有助于提高的编码技能
import csv
def read_csv(filename, outfile="res.csv"):
vowel = {"a", "e", "i", "o", "u"}
with open(filename, 'r') as f, open(outfile, 'w', newline="") as out:
out.write(f.readline()) # write header
reader = csv.reader(f, delimiter=' ', skipinitialspace=True) # space-separate and skip extra spaces
writer = csv.writer(out) # csv write object
# Filter out lines that don't end with a vowel
writer.writerows(line for line in reader if line[1][-1] in vowel)
read_csv("a.csv", outfile="res.csv")