Python - File processing ( Write Method ) -
i having trouble while writing file using python
from validate_email import validate_email result=open('output1.tsv','wb') f=open('input.csv','r') y=[] result.write('email_address\temail_validation\n') in f: y.append(i.replace('\n','')) j in y: try: val=validate_email('%s'%j, verify=true) except: val = "check again" result.write('%s\t%s\n'%(j,val)) print j,val
here variable x has operation , may take time process it.
variable y has more count of 500 ( input file contains 700 rows ).
but after run program around 120 written in output file.
a more idiomatic python is
emails = [l.strip() l in open('input.csv','r').readlines()] valid = [str(validate_email(addr, validate=1)) addr in emails] validated = ['\t'.join(addr_val) addr_val in zip(emails, valid)] of open('output.tsv'): of.write('email_address\temail_validation\n') of.write('\n'.join(validated)) # if needed ; of.write('\n')
edit in response late further info op
to take account possibility of timeouterror
exception, raised smtp
module, can write helper function
def validate_no_timeout(address): try: response = str(validate_email(address, validate=1)) except timeouterror: response = "time out" return response
and rewrite second line in original post read
valid = [validate_no_timeout(addr) addr in emails]
nb: in op see generic reference time out error. in code catched timeouterror
exception, in lack of exact info op.
Comments
Post a Comment