2015-04-30 33 views
0

我有這個腳本我正在使用,我上網和修改了一下。基本上我想要的是刮我的收件箱中的所有電子郵件,並將它們保存爲CSV,電子郵件,名字,姓氏格式。到目前爲止,這是保存所有這些信息爲CSV,而不是三一列...寫電子郵件到CSV導致只有一列

#!/usr/bin/env python 
# 
# Very basic example of using Python 3 and IMAP to iterate over emails in a 
# gmail folder/label. This code is released into the public domain. 
# 
# This script is example code from this blog post: 
# http://www.voidynullness.net/blog/2013/07/25/gmail-email-with-python-via-imap/ 
# 
# This is an updated version of the original -- modified to work with Python 3.4. 
# 
import sys 
import imaplib 
import getpass 
import email 
import email.header 
import datetime 
import csv 


EMAIL_ACCOUNT = "[email protected]" 

# Use 'INBOX' to read inbox. Note that whatever folder is specified, 
# after successfully running this script all emails in that folder 
# will be marked as read. 
EMAIL_FOLDER = "INBOX" 


def process_mailbox(M): 
    """ 
    Do something with emails messages in the folder. 
    For the sake of this example, print some headers. 
    """ 

    rv, data = M.search(None, "ALL") 
    if rv != 'OK': 
     print("No messages found!") 
     return 

    emailList = [] 
    out = open('email_list.csv', 'w') 
    for num in data[0].split(): 
     rv, data = M.fetch(num, '(RFC822)') 
     if rv != 'OK': 
      print("ERROR getting message", num) 
      return 

     msg = email.message_from_bytes(data[0][1]) # Contains all the message info including header and content 
     hdr = email.header.make_header(email.header.decode_header(msg['From'])) 
     sender = str(hdr) 
     emailList.append(sender) 
     #out.write(sender) 
     #out.write('\n') 

    for c in emailList: 
     print (c.split(" ")) 
     out.write(c) 
     out.write('\n') 

    out.close() 


##  print('Raw Date:', msg['Date']) 
##  # Now convert to local date-time 
##  date_tuple = email.utils.parsedate_tz(msg['Date']) 
##  if date_tuple: 
##   local_date = datetime.datetime.fromtimestamp(
##    email.utils.mktime_tz(date_tuple)) 
##   print ("Local Date:", \ 
##    local_date.strftime("%a, %d %b %Y %H:%M:%S")) 


M = imaplib.IMAP4_SSL('imap.gmail.com') 

try: 
    rv, data = M.login(EMAIL_ACCOUNT, 'password') 
except imaplib.IMAP4.error: 
    print ("LOGIN FAILED!!! ") 
    sys.exit(1) 

print(rv, data) 

rv, mailboxes = M.list() 
if rv == 'OK': 
    print("Mailboxes:") 
    print(mailboxes) 

rv, data = M.select(EMAIL_FOLDER) 
if rv == 'OK': 
    print("Processing mailbox...\n") 
    process_mailbox(M) 
    M.close() 
else: 
    print("ERROR: Unable to open mailbox ", rv) 

M.logout() 

其中在這樣的格式打印出來:

['John', 'Doe', '<[email protected]>'] 
['Joe', 'Doe', '<[email protected]>'] 
['Jacob', 'Doe', '<[email protected]>'] 
['Homer', 'Simpson-Doh!', '<[email protected]>'] 

但在CSV它打印的所有信息合併到一列像這樣:

 Column 1 
Row 1 John Doe <[email protected]> 
Row 2 Joe Doe <[email protected]> 
Row 3 Jacob Doe <[email protected]> 

但我需要3列instead..tried做out.write(c.split(」「)),但導致錯誤。

+0

你實際上並沒有一列,我讀到csv模塊的文件時你會有三列 –

+0

真的嗎?我也用記事本打開了它,爲我打開了它的全部專欄。 – ratman2050

回答

相關問題