繁体   English   中英

Python:UnicodeEncodeError:'ascii' 编解码器无法在位置 0 中对字符 '\Ο' 进行编码:序号不在范围内 (128)

[英]Python: UnicodeEncodeError: 'ascii' codec can't encode character '\u039f' in position 0: ordinal not in range(128)

我正在尝试创建一个 Python 脚本,该脚本连接到我为出勤而创建的数据库。 具体来说,脚本的作用是连接到我创建的 SQLite 3 数据库,询问学校的电子邮件和密码,然后询问学生的 ID(当我的 MFRC522 读写器到达时,这将只是一个读取命令木板)。 这是在带有 Raspbian Full 的 Raspberry Pi 4 Model B 4GB 上运行的。 我的代码:

# coding=utf-8
import smtplib #email library
import sqlite3 #SQLite 3 inteface library
import ssl #encryption library
import string

conn = sqlite3.connect ('students') #connect to the database
email = input("Γράψε το email του σχολείου") #ask for email
password = input("Γράψε τον κωδικό για το email του σχολείου: ") #ask for the email password

def sendemail(): #create the sendemail() function
    port = 465  # For SSL
    smtp_server = "smtp.gmail.com"
    message = "Ο μαθητής έφτασε"


    # Create a secure SSL context
    context = ssl.create_default_context()

    with smtplib.SMTP_SSL(smtp_server, port, context=context) as server:
       server.login(email, password)
       server.sendmail(email, remail, message) #remail is the receiver email

c = conn.cursor() #c is a cursor in the database

mathID = input("Γράψε το ID σου: ") #ask for the student's ID
c.execute('SELECT Mail from parousiologio WHERE ID=?', [mathID]) #we tell the c cursor to execute the SQL command to find the email from the matching ID column
remail= c.fetchall() #we select the data from c.execute()
c.execute('Select Onoma from parousiologio where ID=?', [mathID]) #we do the same thing as the student email but now for the student's name.
mathitis = c.fetchall()
conn.close() #disconnect from the database
sendemail() #run the sendemail() function

很抱歉,字符串是希腊语。 我是希腊人,这是我正在做的一个项目。 在发布之前,我也不得不翻译评论。

无论如何,当我执行此代码时:

python3 bash\ dedomenon2.py

我收到标题中的错误。 我也尝试使用PYTHONIOENCODING=utf-8但仍然没有。 我怎样才能解决这个问题? 非常感谢,祝你有美好的一天!

我假设错误在server.sendmail(email, remail, message)行上。

sendmail的文档是明确的(强调我的):

... msg 可以是包含 ASCII 范围内字符的字符串,也可以是字节字符串。 使用 ascii 编解码器将字符串编码为字节,并将单独的 \\r 和 \\n 字符转换为 \\r\\n 字符。 不修改字节字符串。

当您传递包含希腊(非 ascii)字符的消息时,它使用 ASCII 编解码器进行编码会引发错误。

怎么修:

最简单的方法是恕我直言,使用send_message方法:

from email.message import EmailMessage
...
    msg = EmailMessage()
    msg.set_content(message)
    with smtplib.SMTP_SSL(smtp_server, port, context=context) as server:
       server.login(email, password)
       server.send_message(msg, email, remail) #remail is the receiver email

它将自动将消息编码为 utf8 并添加相关标头。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python:UnicodeEncodeError:'ascii'编解码器无法在位置0编码字符u'\\ xfc':序数不在范围内(128)-> Excel Python:UnicodeEncodeError:'ascii'编解码器无法编码位置78中的字符u'\\ xf1':序数不在范围内(128) UnicodeEncodeError:'ascii'编解码器无法对位置448中的字符u'\\ u2013'进行编码:序数不在范围内(128) UnicodeEncodeError:'ascii'编解码器无法在位置32编码字符u'\\ u2019':序数不在范围内(128) UnicodeEncodeError: 'ascii' codec can't encode character u'\–' in position 3 2: ordinal not in range(128) UnicodeEncodeError:'ascii'编解码器无法在位置1处编码字符u'\\ u2730':序数不在范围内(128) UnicodeEncodeError: 'ascii' codec can't encode character u'\’' in position 6: ordinal not in range(128) UnicodeEncodeError:'ascii'编解码器无法对位置126中的字符u'\\ u2019'进行编码:序数不在范围内(128) UnicodeEncodeError:'ascii'编解码器无法对位置34中的字符u'\\ u05a0'进行编码:序数不在范围内(128) UnicodeEncodeError:'ascii'编解码器无法对位置47中的字符u'\\ u2019'进行编码:序数不在范围内(128)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM