[Tutor] Fixing garbled email addresses
bensherman at gmail.com
Tue May 1 16:39:12 CEST 2007
On 5/1/07, Dotan Cohen <dotancohen at gmail.com> wrote:
> I have had the misfortune of having a university Windows machine
> garble all the email addresses in my addressbook (a txt file so that I
> can use it both on my home Fedora machine and on the university
> Windows machines). I figure this is as good a time as any to start
> learning python and fix the file. How can I iteriate through a text
> file that looks like this:
> "=?UTF-8?B?157XqNeZ15Qg15nXoNeY16bXnw==?=" <someuser at t2.technion.ac.il>,
> "=?UTF-8?B?157XqNenINen15nXmNek15XXkQ==?=" <someuser at gmail.com>,
> "=?UTF-8?B?157XqdeUINem15LXkNeZ?=" <someuser at walla.co.il>,
> and have it return:
> someuser at t2.technion.ac.il,
> someuser at gmail.com,
> someuser at walla.co.il,
> Thanks in advance.
> Dotan Cohen
> Tutor maillist - Tutor at python.org
Hi Dotan! Welcome to python!
Here is some code that will do what you need. It uses the re module,
which are regular expressions.
# You need to import the module:
# Then you need to read in the file that contains your list.
email_list = open("brokenemails.txt","r")
# We need to generate your regular expression. The grabs anything in
# the file that is between < and >, but it includes the <>
# Then filter each line of the file through the regex, discarding the
# <> from above, and puts each address into a list.
addresses = [re_mail.search(line).group(1) for line in
# Now we print them out, comma and newline separated
Let me know if you need more detail!
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Tutor