gpt4 book ai didi

python - 使用 python 为 action server 解析电子邮件

转载 作者:行者123 更新时间:2023-11-28 17:46:06 25 4
gpt4 key购买 nike

我在论坛上找到了这个 python 代码,作为对与我的问题相关的问题的回答。我不太了解 python,所以有人可以告诉我为什么这行不通吗?

(一些背景信息:我有一个网络表单,它会自动通过电子邮件发送到 openERP,然后自动创建一个潜在客户。但是,当创建一个潜在客户时,电话和姓名等信息不会从电子邮件中读取并分类到他们在潜在客户表格中的相应字段。)

# 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 <jain.atul43@gmail.com> to jain.atul10@hotmail.com with
Message-Id <CAG=2G76_SRthL3ybGGyx2Lai5H=RMNxUOjRRR=+5-ODrcgtEZw@mail.gmail.com>:
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')})"

请帮助我理解问题。

最佳答案

我已经修复了 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符合 pre_dict[field]=split_line[1]
  3. 在同一行中,您使用了一个变量 pre_dict , 以前没有被引用过。我想你的意思是使用 _dict所以该行应该是_dict[field]=split_line[1]
  4. 函数返回dict这是一种类型,而不是变量。您可能希望它返回包含字段数据的字典,因此它应该返回 _dict反而;否则你总是会得到结果 <type 'dict'>

至于剩下的代码,没有足够的上下文让我理解发生了什么或出了什么问题。至少 parse_description函数现在应该可以工作了。

关于python - 使用 python 为 action server 解析电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17838142/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com