Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /usr/bin/python
- #bcc.py
- #
- #A script to strip email addresses from a file and convert them to a format suitable for pasting into a bcc field
- import sys, string
- def main():
- #infile = open("raw.txt", "r") #to call from within IDE for debugging
- infile = open(sys.argv[1], 'r') #open the file given to the script as an argument
- rawString = infile.read()
- rawString = stripOut(rawString, "\n") #replace newlines with spaces
- rawString = stripOut(rawString, ",") #replace commas with spaces
- addressList = [] #create an empty list to put the addresses in
- for address in string.split(rawString):#break it up into items separated by whitespace
- address = wellFormed(address) #strip it of forbidden elements
- if address !="": #check the result still contains an email address
- addressList.append(address) #if so, add it to the list
- bccString = ", ".join(addressList) #join the addresses together separated by commas
- infile.close() #close the input file
- outfile = open("bcc.txt", "w") #open file to write
- outfile.write(bccString) #write
- outfile.close() #close
- #print bccString #IDE debugging
- def wellFormed(possAddress): #turn input into well-formed email address, or empty string
- at = False #make sure "@" is present
- for char in possAddress:
- if char == "@":
- at = True
- if at == False:
- return "" #if not, return an empty string
- user, domain = possAddress.split("@") #divide into user and domain
- user, domain = stripLeft(user), stripRight(domain) #strip off forbidden characters from beginning and end respectively
- if user == "" or domain == "": #if either is empty, return empty string
- return ""
- else:
- return user + "@" + domain
- def stripLeft(text): #strip inadmissible characters off the neginning of the username
- if text == "":
- return text
- elif alphaNum(text[len(text)-1]) == False:
- return "" #throw away illegal character and anything to its left
- else:
- return stripLeft(text[:-1]) + text[-1] #recursively move leftward if character is legal
- def stripRight(text): #strip inadmissible characters off the end of the domain name
- if text == "":
- return text
- elif alphaNum(text[0]) == False:
- return ""
- else:
- return text[0] + stripRight(text[1:])
- def alphaNum(char): #check if character permitted
- if char == "":
- return False #if empty string
- ascii = ord(char) #get ASCII value of character
- if ascii == 46: #is it a full stop?
- return True
- elif ascii > 64 and ascii < 91: #or a capital letter?
- return True
- elif ascii > 96 and ascii < 123: #lower case?
- return True
- elif ascii > 47 and ascii < 58: #a numeral?
- return True
- else:
- return False #not a permitted character then ...
- def stripOut(mainString, subString):
- return " ".join(mainString.split(subString))#replace occurrences of subString with spaces
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement