从一个csv文件中选取特定的行复制到另一个csv文件中
- 获取指定目录下的所有指定后缀的文件名
import os
path = "D:/肺结节项目/LUNA16/all/subset9"
res_file = []
f_list = os.listdir(path)
for i in f_list:
if os.path.splitext(i)[1] == '.mhd':
res_file.append(os.path.splitext(i)[0])
- 从一个csv文件中选取特定的行复制到另一个csv文件中
file_path1 = 'annotations_excluded.csv'
file_path2 = 'annotations_excluded0.csv'
headers = ['seriesuid', 'coordX', 'coordY', 'coordZ', 'diameter_mm']
rows = []
with open(file_path1) as f:
csv_read = csv.reader(f)
for row in csv_read:
if row[0] in res_file:
rows.append(row)
with open(file_path2, 'w', newline='') as f:
f_csv = csv.writer(f)
f_csv.writerow(headers)
f_csv.writerows(rows)
参考链接1
参考链接2