sachit sachit - 1 year ago 63
Python Question

concat two columns and store in a new column in a csv

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:


# 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])

if __name__=="__main__":
obj = Image()

Answer Source

If you can use Pandas:

import pandas as pd

df = pd.read_csv('rosary.csv')
df['Image'] = df.ImageName + "_" + df.ImageWChain

>>> 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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download