# Dirty conversion of salesforce addresses into # householded lines for mailmerge into letters.# todo: convert commas into linebreaks on first line of address.# todo: ensure that the "CITY" isn't duplicated. importcsvimportrenewNames=[]defAppendName(name1,name2):ifname1.find(" & ")==-1:returnname1+" & "+name2else:returnname2+", "+name1defbuildName(newNames,search):searchStr=search[7]name=""foriteminnewNames:ifitem[7]==searchStr:ifname=="":name=item[8]elifname.find(" & ")==-1:name=name+" & "+item[8]else:name=item[8]+", "+namereturnnamedefremove_duplicates(values):output=[]seen=set()forvalueinvalues:# If value has not been encountered yet,# ... add it to both list and set.ifvalue[7]notinseen:output.append(value)seen.add(value[7])returnoutputwithopen('./members.csv')ascsv_file:csv_reader=csv.reader(csv_file,delimiter=',',quoting=csv.QUOTE_ALL)line_count=0forrowincsv_reader:Salutation=row[2]Firstname=row[3]Surname=row[4]Primary_Street=row[12]Primary_City=row[13]Primary_Postcode=row[14]Primary_County=row[15]Primary_Country=row[16]search_address=Primary_Street+Primary_Postcode+Primary_Citysearch_address=search_address.lower()search_address=search_address.strip()search_address=re.sub(r'\W+','',search_address)member_name=Firstname+" "+Surnameoutput_address=[]output_address.extend([Salutation,Firstname,Surname])output_address.extend([Primary_Street,Primary_City,Primary_Postcode,Primary_County])output_address.append(search_address)output_address.append(member_name)newNames.append(output_address)deduped=remove_duplicates(newNames)foritemindeduped:item[8]=buildName(newNames,item)withopen('./names.csv','w',newline='')ascsvfile:writer=csv.writer(csvfile)foroutlineindeduped:writer.writerow([outline[3],outline[4],outline[5],outline[8]])print(outline[8])
Comments (0)
HTTPSSSH
You can clone a snippet to your computer for local editing.
Learn more.