Python故障u202

2024-06-08 04:24:08 发布

您现在位置:Python中文网/ 问答频道 /正文

首先,坦白-我不是开发者。我的开发人员刚离开,我们需要的一段代码失败了,所以我真的希望这里有人能指出如何修复。基本上,代码查询gmail帐户,从特定格式的电子邮件中提取数据,并以csv文件的形式发送。我相信它是gmail数据中的流氓角色。我的问题是,我如何识别哪封邮件包含流氓角色?我已经缩小了日期范围,所以知道问题存在于250个记录中,但我无法直观地识别哪一个。在

以下是失败消息:

Authentication successful.
Messages total: 257
Traceback (most recent call last):
  File "./extract_billing_from_gmail.py", line 579, in <module>
    csvwriter.writerow([ lead_customer, lead['client_id'], lead['operator_name'], lead['chat_id'], lead['time'], lead['date'], email_time, email_date, timelag_minutes, lead['out_of_hours'], lead['type'],  lead['area_to_rent_or_buy'], lead['area_to_sell_or_let'], lead['name'], lead['email'], lead['phone'], lead['phone2'], email_subject_plaintext ])
UnicodeEncodeError: 'ascii' codec can't encode character u'\u202c' in position 15: ordinal not in range(128)
Admins-MacBook-Pro:OCSL_created_gmail_billing andy$ 

每封电子邮件的格式如下: 主题--Yomdel Live Lead-[客户名称]

然后主体包含以下内容:

操作员姓名:Jane

审核人:奥利弗

客户ID:XXXXX James

时间:上午10:41

日期:2016年1月20日

聊天ID:O1Q86YL8T3

姓名:Sophie XXXXX

电话:07917 000000

电子邮箱:sophie.x。xxxxxx@hotmail.co.uk在

客户类型:买方/卖方

物业类型:3张床(待购)

预算:325000英镑

接下来有几行描述需求。在

非常感谢您的帮助。在


Tags: 数据代码nameinid角色date客户
1条回答
网友
1楼 · 发布于 2024-06-08 04:24:08

这里最简单的方法是将第579行换行到try/except块中并打印出主题,然后重新引发错误:

try:
    csvwriter.writerow([ lead_customer, lead['client_id'], lead['operator_name'], lead['chat_id'], lead['time'], lead['date'], email_time, email_date, timelag_minutes, lead['out_of_hours'], lead['type'],  lead['area_to_rent_or_buy'], lead['area_to_sell_or_let'], lead['name'], lead['email'], lead['phone'], lead['phone2'], email_subject_plaintext ])
except UnicodeEncodeError:
    print 'Failed to parse {}.'.format(email_subject_plaintext.encode('utf-8', 'replace')
    raise

这将有助于您通过主题行识别出有问题的电子邮件。当然,你想要的任何其他识别信息也可以添加进去——关键当然是确保它被正确编码以打印。在

这就涉及到了一个更大的问题,即在将数据位传递给csvwriter之前,似乎应该对它们进行适当的编码——类似于我在print语句中展示的.encode()调用,需要对输出的每一位数据进行处理,这应该允许脚本在遇到奇怪的角色时也能运行。在

相关问题 更多 >

    热门问题