2012-06-21 124 views
1

我有一個用戶名和電子郵件文件,格式如下:蟒蛇解析文件

pete,[email protected] 

我想只保留郵件,所以我想過使用正則表達式是這樣的:

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) 

但是我不知道如何將結果存儲在一個文件中。 我總是得到新文件中的最後一個電子郵件地址。

什麼是將結果存儲在文件中的最佳方式? 謝謝!

+0

是'IP '應該是'MAIL'? – jadkik94

回答

6
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) 
+0

'line.split()'分裂成...? –

+0

@Kirk Strauser:是的,修正了這個問題。 –

+0

爲什麼要剝離「字詞[1]」,然後在其上添加「\ n」?附註:我完全同意你的方法。只是爲了那些可能想知道這些事情的新用戶而挑剔。 :-) –

2

您可以使用csv模塊(因爲您的數據看起來逗號分隔,至少在你的例子):

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]) 
1

嘗試是這樣的:

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() 
+0

1)'address'將是分割後的值列表,2)'file.readlines'在每行末尾保留'\ n'。 –

+0

哼哼代碼回聲a:TypeError:只能連接列表(不是「str」)在地址行上列出 – user1473508

+0

哼,我正在尋找這種類型的輸出文件: [email protected] \ n [email protected] \ n [email protected] \ n 我不需要昏迷! – user1473508