2

I have a file with username and emails, in this format :

pete,[email protected]

I want to only keep the email, so i thought about using a regex like this :

import re,sys

Mailfile = sys.argv[1]

file = open(Mailfile, "r")

for MAIL in file.readlines():
   tmp = re.split("\n+", MAIL)
   m = re.match( ',(.+)', MAIL)
   m.group(0)

But then I don't know how to store the result in a file. I always get the last email address in the new file.

What would be the best way to store the results in a file ? Thanks!

1
  • Is IP supposed to be MAIL? Commented Jun 21, 2012 at 23:02

3 Answers 3

8
import sys

infile, outfile = sys.argv[1], sys.argv[2]

with open(infile) as inf, open(outfile,"w") as outf:
    line_words = (line.split(',') for line in inf)
    outf.writelines(words[1].strip() + '\n' for words in line_words if len(words)>1)
Sign up to request clarification or add additional context in comments.

5 Comments

And line.split() splits on...?
@Kirk Strauser: yep, fixed that.
Why are you stripping words[1] and then adding \n back onto it? Side note: I completely agree with your approach. Just nitpicking for the sake of new users who might be wondering about these things. :-)
@Kirk Strauser: um... general paranoia? If he had any extraneous whitespace in his original file, now he won't.
+1, but I would add (line.split(',') for line in inf if ',' in line) to avoid the error on blank lines for the non existing subscription on words[1]
2

You could use the csv module (since your data looks comma-separated, at least in your example):

import sys
import csv
with open('mail_addresses.txt', 'w') as outfile:
    for row in csv.reader(open(sys.argv[1], 'rb')):
        outfile.write("%s\n" % row[1])

Comments

1

Try something like this:

import sys

Mailfile = sys.argv[1]
Outfile = sys.argv[2]

try:
    in_file = open(Mailfile, 'r')
    out_file = open(Outfile, 'a')

    for mail in in_file.readlines():
        address = mail.split(',')[1].strip()
        out_file.write(address+',') #if you want to use commas to seperate the files, else use something like \n to write a new line.
finally:
    in_file.close()
    out_file.close()

3 Comments

1) address would be a list of values after the split, and 2) file.readlines retains the \n at the end of each line.
hum the code echo a : TypeError: can only concatenate list (not "str") to list on the address line
Hum, I was looking for this type of output in the file: [email protected]\n [email protected]\n [email protected]\n I don't need the coma !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.