This is my first time coding in my life. I don't really know much about coding. I googled whatever I need and combined them all together. It works great, but want to know if there is any improvement needed. Is there a better language to write in?
import os, csv, re, time
from datetime import date, timedelta
SendIDs =[]
BouncedEAes=[]
SubKeys=[]
MerchantIDs=[]
EventDates=[]
BounceCategories=[]
BounceReasones=[]
#SendTimes=[] #To find the most recent date that a welcome email was sent.
BounceDate=0
f = open("SendJobs.csv") #To get Send IDs and dates for Welcome emails
for row in csv.reader(f):
if "Welcome" in row[7]:
SendIDs.append(row[1])
# SendTimes.append(
# time.strftime("%Y%m%d",
# time.strptime(row[5],"%m/%d/%Y %H:%M:%S %p")))
f.close()
# if not os.path.exists('SendIDs.csv'):
# open('SendIDs.csv', 'w').close()
# f = open("SendIDs.csv")
# for row in csv.reader(f):
# SendIDs.append(row[0])
# UniqSendIDs = {}.fromkeys(SendIDs).keys()
# f.close()
# f = open("SendIDs.csv","w")
# with f as output:
# writer = csv.writer(output, lineterminator='\n')
# for item in UniqSendIDs:
# writer.writerow([item])
# f.close()
f = open('Bounces.csv')
for row in csv.reader(f):
for item in SendIDs: #OR UniqSendIDs
if item == row[1]:
SubKeys.append(row[2])
BouncedEAes.append(row[3])
BounceCategories.append(row[8])
BounceReasones.append(row[10])
#EventDate: Only need bounce date, NO time required.
BounceDate = time.strptime(row[6],"%m/%d/%Y %H:%M:%S %p")
BounceDate = time.strftime("%m/%d/%Y", BounceDate)
EventDates.append(BounceDate)
f.close()
f = open('Attributes.csv')
for row in csv.reader(f):
for item in BouncedEAes:
if item == row[2]:
MerchantIDs.append(row[4])
f.close()
SubKeys.insert(0,"SubscriberKey")
BouncedEAes.insert(0,"EmailAddress")
MerchantIDs.insert(0,"Merchant_Number")
EventDates.insert(0,"EventDate")
BounceCategories.insert(0,"BounceCategory")
BounceReasones.insert(0,"BounceReason")
new_data=[[SubKeys[i],BouncedEAes[i], MerchantIDs[i], EventDates[i], BounceCategories[i], BounceReasones[i]] for i in range(len(BouncedEAes))]
# if len(SendTimes)==0:
# yesterday = date.today() - timedelta(1)
# SendTimes.append("NoWelcomeEmailsSent_"+yesterday.strftime("%Y%m%d"))
# if len(BouncedEAes)==1:
# SendTimes.append("NoBouncedEmails_"+SendTimes[-1])
# FILENAME FORMAT: Welcome-Cadence-#2-RESULT-YYYYMMDD
# YYYYMMDD: One day before the day the file was received.
yesterday = date.today() - timedelta(1)
f=open("Welcome-"+yesterday.strftime('%Y%m%d')+".csv","wb")
output=csv.writer(f)
for row in new_data:
output.writerow(row)
f.close()
"""
ERROR MESSAGES
"""
#new_data=[[SubKeys[i],BouncedEAes[i], MerchantIDs[i], EventDates[i], BounceCategories[i], BounceReasones[i]] for i in range(len(BouncedEAes))]
#IndexError: list index out of range
"""
Check Attributes.csv
"""