Python Unicode 麻烦 - 如何将文本文件用作电子邮件正文?
Python Unicode troubles - How can I use a text file as an e-mail body?
真令人惊讶 又一个遇到 Unicode 问题的人。
我的文本被复制到我的电子邮件中,但只是在被编码为 utf-8 无数次之后 - 即便如此,它还是混合了 50/50 的乱码。
过去我通过使用 pathlibs 编码避免了这种麻烦,但在这里似乎不可能。我认为这对你们来说是最重要的代码。
#Write info to text file
with io.open(f'today.txt', "a", encoding = 'utf-8') as wf:
wf.write(str(today) + '\n')
wf.write(item + '\n')
wf.write(text + '\n\n')
wf.write(urlbuff + href + '\n\n\n')
#Store text for e-mail use
with io.open(f'today.txt', "r+", encoding = 'utf-8') as nwf:
text_to_mail = str(nwf.readlines())
text_mail = text_to_mail.encode('utf-8', 'strict')
我的 text_mail 输出如下所示:
b'['2021-01-22\n', 'Wealth tax\n', "A wealth tax isn't perfect but it's not Armageddon\n", '\n'
如果我不使用所有单独的编码命令,我会收到 ascii 错误并且没有输出。我该如何解决这个问题,并确保它再也不会发生在我身上?我确信一定有比我一直在尝试的更清洁、更明智的解决方法。我正在使用 smtplib 发送电子邮件。
我知道有很多代码您看不到,但我希望这里已经足够了。
感谢您的评论,我将 with 语句更改为:
with io.open(f'today.txt', "r") as nwf:
text_mail = nwf.read()
我还必须更改我电脑的 AppData 目录中的 smtplib 文件,以便使用 .encode('utf-8') 代替一些 ascii .encode() 语句。我所做的更改如下:
self.putcmd("data")
(code, repl) = self.getreply()
if self.debuglevel > 0:
self._print_debug('data:', (code, repl))
if code != 354:
raise SMTPDataError(code, repl)
else:
if isinstance(msg, str):
msg = _fix_eols(msg).encode('ascii')
至:
self.putcmd("data")
(code, repl) = self.getreply()
if self.debuglevel > 0:
self._print_debug('data:', (code, repl))
if code != 354:
raise SMTPDataError(code, repl)
else:
if isinstance(msg, str):
msg = _fix_eols(msg).encode('utf-8')
如果有人需要自己搜索 'ascii',请更改上面的“msg = _fix_eols(msg).encode('ascii')”语句,我相信那里有两个。我并不完全了解上述更改的所有可能影响,因此另一个修复可能会更好。
真令人惊讶 又一个遇到 Unicode 问题的人。
我的文本被复制到我的电子邮件中,但只是在被编码为 utf-8 无数次之后 - 即便如此,它还是混合了 50/50 的乱码。
过去我通过使用 pathlibs 编码避免了这种麻烦,但在这里似乎不可能。我认为这对你们来说是最重要的代码。
#Write info to text file
with io.open(f'today.txt', "a", encoding = 'utf-8') as wf:
wf.write(str(today) + '\n')
wf.write(item + '\n')
wf.write(text + '\n\n')
wf.write(urlbuff + href + '\n\n\n')
#Store text for e-mail use
with io.open(f'today.txt', "r+", encoding = 'utf-8') as nwf:
text_to_mail = str(nwf.readlines())
text_mail = text_to_mail.encode('utf-8', 'strict')
我的 text_mail 输出如下所示:
b'['2021-01-22\n', 'Wealth tax\n', "A wealth tax isn't perfect but it's not Armageddon\n", '\n'
如果我不使用所有单独的编码命令,我会收到 ascii 错误并且没有输出。我该如何解决这个问题,并确保它再也不会发生在我身上?我确信一定有比我一直在尝试的更清洁、更明智的解决方法。我正在使用 smtplib 发送电子邮件。
我知道有很多代码您看不到,但我希望这里已经足够了。
感谢您的评论,我将 with 语句更改为:
with io.open(f'today.txt', "r") as nwf:
text_mail = nwf.read()
我还必须更改我电脑的 AppData 目录中的 smtplib 文件,以便使用 .encode('utf-8') 代替一些 ascii .encode() 语句。我所做的更改如下:
self.putcmd("data")
(code, repl) = self.getreply()
if self.debuglevel > 0:
self._print_debug('data:', (code, repl))
if code != 354:
raise SMTPDataError(code, repl)
else:
if isinstance(msg, str):
msg = _fix_eols(msg).encode('ascii')
至:
self.putcmd("data")
(code, repl) = self.getreply()
if self.debuglevel > 0:
self._print_debug('data:', (code, repl))
if code != 354:
raise SMTPDataError(code, repl)
else:
if isinstance(msg, str):
msg = _fix_eols(msg).encode('utf-8')
如果有人需要自己搜索 'ascii',请更改上面的“msg = _fix_eols(msg).encode('ascii')”语句,我相信那里有两个。我并不完全了解上述更改的所有可能影响,因此另一个修复可能会更好。