如何在python中将csv文件的列与图像名称进行比较? [英] How to compare a column of a csv file with an image name in python?
本文介绍了如何在python中将csv文件的列与图像名称进行比较?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个CSV文件,其中列出了每个文件都有ID号的图像文件,如下所示:
I have a CSV file that lists image files with an ID number for each, like this:
21_cook.png 9
35_read.png 6
13_walk.png 2
图像文件位于文件夹中. 下面的代码搜索出图像文件的名称:
The image files are in a folder. The code below searches out the image file names:
for dirname, dirnames, filenames in os.walk('./images'):
for filename in filenames:
if filename.endswith('.png'):
names.append(filename)
这部分有效.
此代码从CSV文件中提取图像名称:
This code extracts the image names from the CSV file:
file = open('namesIma.csv')
lns = csv.reader(file)
for line in lns:
nam = line [0]
names1.append(nam)
如何打印图像文件夹和CSV文件的索引中的每个文件的ID号.
How can I print the ID number of each file that's in both the images folder and the CSV file's index.
推荐答案
在您的 for 循环中为名称添加一个子句:
Add a clause to your for loop over the names:
for line in lns:
nam = line [0]
names1.append(nam)
if nam in names:
print (nam, line[1])
这能为您解决问题吗?
这篇关于如何在python中将csv文件的列与图像名称进行比较?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文