简体   繁体   English

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

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

I am trying to create a Python script that connects to a database I have created for attendance.我正在尝试创建一个 Python 脚本,该脚本连接到我为出勤而创建的数据库。 Specifically what the script does is connect to the SQLite 3 database that I have created, ask for the school's email and password, and then ask the student for his ID (when my MFRC522 reader/writer arrives this will be just a read command to the board).具体来说,脚本的作用是连接到我创建的 SQLite 3 数据库,询问学校的电子邮件和密码,然后询问学生的 ID(当我的 MFRC522 读写器到达时,这将只是一个读取命令木板)。 This is running on a Raspberry Pi 4 Model B 4GB with Raspbian Full.这是在带有 Raspbian Full 的 Raspberry Pi 4 Model B 4GB 上运行的。 My code:我的代码:

# 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

Sorry for the fact that the strings are in Greek.很抱歉,字符串是希腊语。 I am Greek and this is a project that I am making.我是希腊人,这是我正在做的一个项目。 I had to translate the comments too before posting.在发布之前,我也不得不翻译评论。

Anyway, when I execute this code with:无论如何,当我执行此代码时:

python3 bash\ dedomenon2.py

I get the error in the title.我收到标题中的错误。 I also tried using PYTHONIOENCODING=utf-8 but still nothing.我也尝试使用PYTHONIOENCODING=utf-8但仍然没有。 How can I fix this?我怎样才能解决这个问题? Thanks a lot and have a great day!非常感谢,祝你有美好的一天!

I assume that the error is on the server.sendmail(email, remail, message) line.我假设错误在server.sendmail(email, remail, message)行上。

The documentation for sendmail is explicit (emphazise mine): sendmail的文档是明确的(强调我的):

... msg may be a string containing characters in the ASCII range , or a byte string. ... msg 可以是包含 ASCII 范围内字符的字符串,也可以是字节字符串。 A string is encoded to bytes using the ascii codec , and lone \\r and \\n characters are converted to \\r\\n characters.使用 ascii 编解码器将字符串编码为字节,并将单独的 \\r 和 \\n 字符转换为 \\r\\n 字符。 A byte string is not modified.不修改字节字符串。

As you pass a message containing Greek (non ascii) characters, its encoding with the ASCII codec raises the error.当您传递包含希腊(非 ascii)字符的消息时,它使用 ASCII 编解码器进行编码会引发错误。

How to fix:怎么修:

The simplest way is IMHO to use the send_message method:最简单的方法是恕我直言,使用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

It will automatically encode the message in utf8 and add the relevant headers.它将自动将消息编码为 utf8 并添加相关标头。

暂无
暂无

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

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