2013-07-24 29 views
3

我在論壇上發現這個python代碼作爲與我的問題有關的東西的答案。我真的不懂Python,所以有人可以告訴我爲什麼這不起作用?使用python解析電子郵件的行動服務器

(一些背景信息:我有一個自動通過電子郵件發送到openERP的Web表單,然後自動創建一個潛在客戶,但是當創建潛在客戶時,電話和姓名等信息不會從電子郵件中讀取並進行排序到領先的形式對相關領域)

# You can use the following variables: 
# - self: ORM model of the record on which the action is triggered 
# - object: browse_record of the record on which the action is triggered if there is one, otherwise None 
# - pool: ORM model pool (i.e. self.pool) 
# - time: Python time module 
# - cr: database cursor 
# - uid: current user id 
# - context: current context 
# If you plan to return an action, assign: action = {...} 

def parse_description(description): 
    ''' 
    there is parse function 
    It is example for parsing messages like this: 

    Name: John 
    Phone: +100500 
    ''' 
    fields=['Name','Phone'] 
    _dict={} 
    description=description.lower() 
    for line in description.split('\n'): 
    for field in fields: 
     if field in line: 
      split_line=line.split(':') 
      if len(split_line)>1: 
       pre_dict[field]=line.split(':')[1] 
    return dict 

lead=self.browse(cr,uid,context['active_id'],context=context) 
description=lead['description'] 
_dict=parse_description(description) 
self.write(cr,uid,context['active_id'],{ 
         'partner_name':_dict.get('name'), 
         'contact_name':_dict.get('name'), 
         'phone':_dict.get(u'phone'), 
         'mobile':_dict.get(u'phone')}) 

更新:

我這些回溯,而我提取郵件

2014-07-01 13:39:40,188 4992 INFO v8_demo openerp.addons.mail.mail_thread: Routing 
mail from Atul Jain <[email protected]> to [email protected] with 
Message-Id <[email protected]om>: 
fallback to model:crm.lead, thread_id:False, custom_values:None, uid:1 
2014-07-01 13:39:40,445 4992 ERROR v8_demo openerp.addons.fetchmail.fetchmail: 
Failed to fetch mail from imap server Gmail. 
Traceback (most recent call last): 
File "/home/atul/openerp-8/openerp/addons/fetchmail/fetchmail.py", line 206, in 
fetch_mail 
action_pool.run(cr, uid, [server.action_id.id], {'active_id': res_id, 'active_ids' 
:[res_id], 'active_model': context.get("thread_model", server.object_id.model)}) 
File "/home/atul/openerp-8/openerp/addons/base/ir/ir_actions.py", line 967, in run 
res = func(cr, uid, action, eval_context=eval_context, context=run_context) 
File "/home/atul/openerp-8/openerp/addons/base/ir/ir_actions.py", line 805, 
in run_action_code_multi 
eval(action.code.strip(), eval_context, mode="exec", nocopy=True) # nocopy allows 
to return 'action' 
File "/home/atul/openerp-8/openerp/tools/safe_eval.py", line 254, in safe_eval 
return eval(c, globals_dict, locals_dict) 
File "", line 14, in <module> 
File "", line 4, in parse_description 
ValueError: "'bool' object has no attribute 'lower'" while evaluating 
u"def parse_description(description): 
fields=['name','phone'] 
_dict={} 
description=description.lower() 
for line in description.split('\\n'): 
for field in fields: 
if field in line: 
split_line=line.split(':') 
if len(split_line)>1: 
_dict[field]=split_line[1] 
return _dict 
lead=self.browse(cr,uid,context['active_id'],context=context)\ndescription=lead['description'] 
_dict=parse_description(description) 

self.write(cr,uid,context['active_id'],{    'partner_name':_dict.get('name'),        'contact_name':_dict.get('name'),   
'phone':_dict.get(u'phone'), 
'mobile':_dict.get(u'phone')})" 

請幫我理解這個問題。

+0

你看過你正在解析的電子郵件的原始數據嗎?電子郵件可能是一個棘手的事情來解析。或者,與此息息相關的是以下幾點:你將什麼作爲參數「描述」傳遞給函數? – erewok

回答

2

我已經解決了parse_description功能:

def parse_description(description): 
    ''' 
    there is parse function 
    It is example for parsing messages like this: 

    Name: John 
    Phone: +100500 
    ''' 
    fields=['name','phone'] 
    _dict={} 
    description=description.lower() 
    for line in description.split('\n'): 
    for field in fields: 
     if field in line: 
      split_line=line.split(':') 
      if len(split_line)>1: 
       _dict[field]=split_line[1] 
    return _dict 
  1. 我改變了fields值,因爲在描述所有操作都在description.lower()爲小寫。
  2. 在線pre_dict[field]=line.split(':')[1],你是分裂線來獲得你的結果。這已經完成了:split_line=line.split(':')所以你可以用pre_dict[field]=split_line[1]
  3. 替換pre_dict行。在同一行上,你使用了一個變量,pre_dict,它以前沒有被引用過。我認爲你的意思是使用_dict所以行應該是_dict[field]=split_line[1]
  4. 函數返回dict這是一個類型,而不是一個變量。您可能希望它返回包含字段數據的字典,因此它應該返回_dict;否則你總是會得到結果<type 'dict'>

至於剩餘的代碼,沒有足夠的上下文讓我理解發生了什麼或錯在哪裏。至少parse_description函數應該現在工作。

+0

它的工作!非常感謝!你的解釋也幫助我學習了更多關於python的知識,所以謝謝你花時間打出來! – user2189190

+0

一個簡單的問題:你的u是什麼意思?爲什麼名字不需要它? – user2189190

+1

「u」表示unicode,在這種情況下不需要。關於unicode的一篇很好的文章是[Joel's](http://www.joelonsoftware.com/articles/Unicode.html)。 –