[Tutor] Fixing garbled email addresses

Ben Sherman 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
>
> http://lyricslist.com/
> http://what-is-what.com/
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor


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:
import re

# 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 <>
re_mail=re.compile(r"\<(.*)\>")

# 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
email_list.readlines()]

# Now we print them out, comma and newline separated
print ",\n".join(addresses)

Let me know if you need more detail!

Your pal,
Ben
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20070501/4dd28356/attachment.html 


More information about the Tutor mailing list