Reputation: 23
i am not getting the desired expected output.I have to concat "itemnumber" column with "imagename" column and save into another column "image" in that csv. Eg B001_B001 .It also creates a duplicate image "B001_B001.jpg" in the image folder.
My input is:
ItemNumber UPC Keyword ImageName ImageWChain Description
0 B001 123 XYZ B001 B001 Test1
1 B002 456 GDH B002 B002 Test2
2 B003 789 GFR B003 B003 Test3
and my expected output is:
ItemNumber UPC Keyword ImageName ImageWChain Description Image
0 B001 123 XYZ B001 B001 Test1 B001_B001
1 B002 456 GDH B002 B002 Test2 B002_B002
2 B003 789 GFR B003 B003 Test3 B003_B003
This is what I have so far:
import csv
import os
from os.path import splitext # splits name & extension from a file
import shutil # making a duplicate copy of a file
import logging
class Image:
def image_fix(self):
#open and read csv
with open('rosary.csv') as csvfile:
#create list for sku, old_imagename_column_1, old_imagename_column_1
#and renamed new_imagename
keyList = [] #for ItemNumber
itemList = [] #for ImageName
itemList1 = [] #for ImageWChain
renamedList = [] #for Renamed NEW_ImageName
spamreader = csv.reader(csvfile, delimiter=",")
#storing all the columns into list
for row in spamreader:
keyList.append(row[0])
itemList.append(row[3])
itemList1.append(row[4])
renamedList.append(row[6])
# processing for ImageName
for item in enumerate(itemList):
oldFileName = name
newFileName = '{}_{}{}'.format(name, keyList[item[0]])
# check if the image file existsin image folder for ImageName column
if (os.path.isfile(oldFileName)):
shutil.copy2(oldFileName, newFileName) #make a duplicate image of every itemname
renamedList[item[0]] = '{}_{}{}'.format(oldFileName, keyList[item[0]])
# write the final output in new csv
with open('rosary.csv','wb') as my_csv:
csvWriter = csv.writer(my_csv,delimiter=',')
for row in zip(keyList, itemList, itemList1, renamedList):
# printing row in order
print(row[0] + '\t' + '\t' + row[3] + '\t' + '\t' +
row[4] + '\t' + '\t' + row[6])
csvWriter.writerow(row)
if __name__=="__main__":
obj = Image()
obj.image_fix()
Upvotes: 1
Views: 3543
Reputation: 6841
A more general solution improving on Alexander's answer, which would work for all column names and all data types, and drop the index in the output file:
import pandas as pd
df = pd.read_csv('rosary.csv')
df['Image'] = df['ImageName'].astype(str) + "_" + df['ImageWChain'].astype(str)
df.to_csv('rosary.csv', index=False)
Upvotes: 1
Reputation: 109706
If you can use Pandas:
import pandas as pd
df = pd.read_csv('rosary.csv')
df['Image'] = df.ImageName + "_" + df.ImageWChain
df.to_csv('rosary.csv')
>>> df
Unnamed: 0 Description ImageName ImageWChain ItemNumber Keyword UPC Image
0 0 Test1 B001 B001 B001 XYZ 123 B001_B001
1 1 Test2 B002 B002 B002 GDH 456 B002_B002
2 2 Test3 B003 B003 B003 GFR 789 B003_B003
To make a new image copy:
for old_image, new_image in zip(df.ImageName, df.Image):
if (os.path.isfile(old_image)):
shutil.copy2(old_image, new_image)
Upvotes: 1