patternpythonMinor
CSV email script efficiency
Viewed 0 times
scriptcsvefficiencyemail
Problem
I'm tasked with getting emails from a .csv file and using them to submit a form. I am using the csv and mechanize Python libraries to achieve this.
The code works, but I am very much a beginner in Python.
I was wondering whether I have any inefficiencies that you can help me get rid of and optimize the script?
import re
import mechanize
import csv
def auto_email(email):
br = mechanize.Browser()
br.open("URL")
br.select_form(name="vote_session")
br['email_add'] = '%s' % (email) #email address
br.submit()
def csv_emails():
ifile = open('emails.csv', "rb")
reader = csv.reader(ifile)
rownum = 1
for row in reader:
auto_email(row[0])
print "%d - %s processed" %(rownum, row[0])
rownum += 1
print 'List processed. You are done.'
ifile.close()
print csv_emails()The code works, but I am very much a beginner in Python.
I was wondering whether I have any inefficiencies that you can help me get rid of and optimize the script?
Solution
I would suggest to use
see here for details: http://www.python.org/dev/peps/pep-0343/
in this case you don't need to do
instead of incrementing rownum by yourself, you can use
I don't see the reason to do this
instead of
with open('emails.csv', "rb") as ifilesee here for details: http://www.python.org/dev/peps/pep-0343/
in this case you don't need to do
ifile.close at the endinstead of incrementing rownum by yourself, you can use
for row, rownum in enumerate(reader):I don't see the reason to do this
br['email_add'] = '%s' % (email) #email addressinstead of
br['email_add'] = emailCode Snippets
with open('emails.csv', "rb") as ifilefor row, rownum in enumerate(reader):br['email_add'] = '%s' % (email) #email addressbr['email_add'] = emailContext
StackExchange Code Review Q#20548, answer score: 6
Revisions (0)
No revisions yet.