Python 3:UnicodeEncodeError:';ascii';编解码器可以';t编码字符'\u0142';在位置111:序号不在范围内(128)
我是Python的新用户 我正在尝试编写脚本,它将帮助我通过我的gmail帐户发送电子邮件。Python从.xlsx文件读取数据,并从excel列表向用户发送消息 在我尝试发送带有波兰语字符的电子邮件之前,一切都正常。 在我的excel电子表格中使用波兰语字符时也会出现同样的问题 我试图使用“编码:utf-8”信息,但它不起作用 我的代码:Python 3:UnicodeEncodeError:';ascii';编解码器可以';t编码字符'\u0142';在位置111:序号不在范围内(128),python,python-3.x,Python,Python 3.x,我是Python的新用户 我正在尝试编写脚本,它将帮助我通过我的gmail帐户发送电子邮件。Python从.xlsx文件读取数据,并从excel列表向用户发送消息 在我尝试发送带有波兰语字符的电子邮件之前,一切都正常。 在我的excel电子表格中使用波兰语字符时也会出现同样的问题 我试图使用“编码:utf-8”信息,但它不起作用 我的代码: # -*- encoding: utf-8 -*- #! python3 # sendDuesReminders.py - Sends emails ba
# -*- encoding: utf-8 -*-
#! python3
# sendDuesReminders.py - Sends emails based on payment status in
spreadsheet.
import openpyxl, smtplib, sys
# Open the spreadsheet and get the latest dues status.
wb = openpyxl.load_workbook('duesRecords.xlsx')
sheet = wb.get_sheet_by_name('Sheet1')
lastCol = sheet.max_column
latestMonth = sheet.cell(row=1, column=lastCol).value
# Check each member's payment status.
unpaidMembers = {}
for r in range(2, sheet.max_row + 1):
payment = sheet.cell(row=r, column=lastCol).value
if payment != 'zaplacone':
name = sheet.cell(row=r, column=2).value
lastname = sheet.cell(row=r, column=3).value
email = sheet.cell(row=r, column=4).value
school = sheet.cell(row=r, column=6).value
rate = sheet.cell(row=r, column=7).value
unpaidMembers[name] = lastname, email, school, rate
# Log in to email account.
smtpObj = smtplib.SMTP_SSL('smtp.gmail.com', 465)
smtpObj.ehlo()
# smtpObj.starttls()
smtpObj.login('xxx@xxx.com', '')
# Send out reminder emails.
for name, (lastname, email, school, rate) in unpaidMembers.items():
body = "Subject: %s - przypomnienie o platnosci raty za treningi GIT Parkour. " \
"\n\nPrzypominamy o uregulowaniu wpłaty za uczestnictwo: %s %s w treningach GIT Parkour w %s." \
"\n\nWplata dotyczy okresu: %s." \
"\n\n\nProsimy o uregulowanie platnosci w kwocie %szl na konto:" \
"\nJESTEM GIT" \
"\nJ.Klos, M.Kolodziejczyk s.c." \
"\nul. Grunwaldzka 609d/17, Gdansk" \
"\nAlior Bank - 36 2490 0005 0000 4500 1296 0191" \
"\n\n\nPozdrawiamy," \
"\nGIT Trenerzy."%(latestMonth, name, lastname, school, latestMonth, rate)
print('Sending email to %s...' % email)
sendmailStatus = smtpObj.sendmail('xxx@xxx.com', email, body)
if sendmailStatus != {}:
print('There was a problem sending email to %s: %s' % (email,
sendmailStatus))
smtpObj.quit()
我收到的错误是:
Traceback (most recent call last):
File "sendDuesEmailReminder.py", line 57, in <module>
sendmailStatus = smtpObj.sendmail('xxx@xxx.com', email, body)
File "/Users/klos/anaconda/lib/python3.6/smtplib.py", line 854, in sendmail
msg = _fix_eols(msg).encode('ascii')
UnicodeEncodeError: 'ascii' codec can't encode character '\u0142' in position 111: ordinal not in range(128)
回溯(最近一次呼叫最后一次):
文件“sendduemailremementer.py”,第57行,在
sendmailStatus=smtpObj.sendmail('xxx@xxx.com",电邮,正文)
sendmail中的文件“/Users/klos/anaconda/lib/python3.6/smtplib.py”,第854行
msg=\u fix\u eols(msg).encode('ascii')
UnicodeEncodeError:“ascii”编解码器无法对位置111中的字符“\u0142”进行编码:序号不在范围内(128)
如果有任何帮助,我将不胜感激。可能与此相关:事实上,更好的是:阅读以下答案:。可能相关:事实上,更好的是:阅读以下答案:。