通过重命名修复csv列中具有相同图像的不同ID?

时间:2016-07-25 14:10:00

标签: python

作为一名新的Python程序员,我无法弄清楚如何完成以下操作。

从csv文件中获取此输入数据:

Sku     Image_Name
B001    a.jpg
B002    a.jpg
B001    b.jpg
B002    c.jpg
B003    x.jpg

多个Sku可能具有相同的图像名称。发生这种情况时,我想通过将Image__Name + "_"值连接到同一行中的图像名称来重命名Sku列中的图像名称。

所以期望的输出数据是:

Sku     Image_Name
B001    a_B001.jpg
B002    a_B002.jpg
B001    b.jpg
B002    c.jpg
B003    x.jpg

之后,它应该根据Image_Name列重命名图像文件夹中的图像。

到目前为止,这就是我的全部内容:

import csv

#open and store the csv file
with open('D:\\test.csv', 'rb') as csvfile:
    spamreader = csv.reader(csvfile, delimiter=',')

2 个答案:

答案 0 :(得分:0)

好的,你还有很长的路要走,但这应该会给你一些关于如何继续的提示(假设文件数量合理):

import csv
from os.path import splitext

with open("/tmp/test.csv", 'rb') as csvfile:
    itemList = []
    renamedList = []
    keyList = []
    spamreader = csv.reader(csvfile, delimiter=",")
    for row in spamreader:
        keyList.append(row[0])
        itemList.append(row[1])
        renamedList.append(row[1])
toBeChanged = [itemNum for itemNum, item in enumerate(itemList) 
                if itemList.count(item) > 1]
for itemNum in toBeChanged:
    name, ext = splitext(itemList[itemNum])
    renamedList[itemNum] = '{}_{}{}'.format(name, keyList[itemNum], ext)

# At this point we have your desired info and can print it just like you 
# have above
print("Sku\tImage_Name")
for row in zip(keyList, itemList):
    print(row[0] + '\t' + row[1])

# Duplicating / renaming files is next. This isn't the only way
# to do it (or the most efficient), but it's an easy way to understand.
# The idea is to first make copies of all needed files...
from shutil import copyfile
changedNames = []
for itemNum in toBeChanged:
    copyfile(itemList[itemNum], renamedList[itemNum])
    changedNames.append(itemList[itemNum])
# ...and then delete the originals. The set is used to eliminate
# duplicates.
from os import remove
for item in set(changedNames):
    remove(itemName)

很多方法可以改进此代码。这里的目的是让它更容易理解。先了解它,然后再改进它。

答案 1 :(得分:0)

import csv
import os
from os.path import splitext # splits name & extension from a file
import shutil   #making a duplicate copy of a file
from os import rename


#open and read csv
with open('test.csv') as csvfile:

#create list for sku,old_imagename and new imagename
    itemList = []
    renamedList = []
    keyList = []
    spamreader = csv.reader(csvfile, delimiter=",")

#processing every row at a time
    for row in spamreader:
        keyList.append(row[0])              #for sku
        itemList.append(row[1])             #for old_imagename
        renamedList.append(row[1])          #for new_imagename


#Processing only sku having same images
toBeChanged = [itemNum for itemNum, item in enumerate(itemList)
                if itemList.count(item) > 1]

for itemNum in toBeChanged:
    name, ext = splitext(itemList[itemNum])  # splitting image name & extension: eg a-> "a" & "jpg"
    oldFileName = name + ext
    print("oldFileName = " + oldFileName)    # oldFileName = a.jpg
    newFileName = '{}_{}{}'.format(name, keyList[itemNum], ext)
    print("newFileName = " + newFileName)    # newFileName = a_B001.jpg & a_B002.jpg


# check if the Image file exists,
    if(os.path.isfile(oldFileName)):
        shutil.copy2(oldFileName, newFileName);        # creating a duplicate image file
        renamedList[itemNum] = '{}_{}{}'.format(name, keyList[itemNum], ext)  #a_B001.jpg
        # os.remove(oldFileName)


#write the final output in new csv
with open('newCsv.csv','w') as mycsv:
    csvWriter = csv.writer(mycsv,delimiter=",")
    for row in zip(keyList, renamedList):
        print(row[0] + '\t' + '\t' + row[1])
        csvWriter.writerow([row[0],row[1]])