使用Python发送数据帧在Python中工作,而不是在Spark/DataRicks中
因此,我有这段代码,它在Python中工作得非常好(多亏了StackOverflow的一些帮助),我想把它放到Databricks中 在生产环境中,导入MICS将是一个数据帧,其中包含我们将发送给用户的一些错误代码 当我尝试在Spark/Databricks中运行编码时,会出现错误代码,但我不确定为什么会出现这种情况 对于为什么python代码可以在python中工作而不能在Databricks中工作,如果您能提供任何帮助,我们将不胜感激 当我尝试使用下面的示例数据运行它时,出现以下错误:使用Python发送数据帧在Python中工作,而不是在Spark/DataRicks中,python,pyspark,databricks,Python,Pyspark,Databricks,因此,我有这段代码,它在Python中工作得非常好(多亏了StackOverflow的一些帮助),我想把它放到Databricks中 在生产环境中,导入MICS将是一个数据帧,其中包含我们将发送给用户的一些错误代码 当我尝试在Spark/Databricks中运行编码时,会出现错误代码,但我不确定为什么会出现这种情况 对于为什么python代码可以在python中工作而不能在Databricks中工作,如果您能提供任何帮助,我们将不胜感激 当我尝试使用下面的示例数据运行它时,出现以下错误:
UnicodeEncodeError: 'ascii' codec can't encode character u'\ufffd' in position 8091: ordinal not in range(128)
---------------------------------------------------------------------------
UnicodeEncodeError Traceback (most recent call last)
<ipython-input-11-e3342ca6d73a> in <module>()
28
29
---> 30 msg = MIMEText(content, text_subtype)
31 msg['Subject']= subject
32 msg['From'] = sender # some SMTP servers will do this automatically, not all
/usr/lib/python2.7/email/mime/text.pyc in __init__(self, _text, _subtype, _charset)
28 MIMENonMultipart.__init__(self, 'text', _subtype,
29 **{'charset': _charset})
---> 30 self.set_payload(_text, _charset)
/usr/lib/python2.7/email/message.pyc in set_payload(self, payload, charset)
224 self._payload = payload
225 if charset is not None:
--> 226 self.set_charset(charset)
227
228 def set_charset(self, charset):
/usr/lib/python2.7/email/message.pyc in set_charset(self, charset)
260 self.set_param('charset', charset.get_output_charset())
261 if isinstance(self._payload, unicode):
--> 262 self._payload = self._payload.encode(charset.output_charset)
263 if str(charset) != charset.get_output_charset():
264 self._payload = charset.body_encode(self._payload)
UnicodeEncodeError: 'ascii' codec can't encode character u'\ufffd' in position 8091: ordinal not in range(128)
AttributeError: 'DataFrame' object has no attribute 'encode'
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-9-ed199c64eb83> in <module>()
21
22 #msg = MIMEText(content, text_subtype)
---> 23 msg = MIMEText(content)
24 msg['Subject']= subject
25 msg['From'] = sender # some SMTP servers will do this automatically, not all
/usr/lib/python2.7/email/mime/text.pyc in __init__(self, _text, _subtype, _charset)
28 MIMENonMultipart.__init__(self, 'text', _subtype,
29 **{'charset': _charset})
---> 30 self.set_payload(_text, _charset)
/usr/lib/python2.7/email/message.pyc in set_payload(self, payload, charset)
224 self._payload = payload
225 if charset is not None:
--> 226 self.set_charset(charset)
227
228 def set_charset(self, charset):
/usr/lib/python2.7/email/message.pyc in set_charset(self, charset)
import pandas as pd
Import_MICS="https://www.iso20022.org/sites/default/files/ISO10383_MIC/ISO10383_MIC.csv"
MICS=pd.read_csv(Import_MICS,encoding = 'ISO-8859-1')
SMTPserver = 'email-smtp.us-east-2.amazonaws.com'
sender = 'x.com'
destination = ['x.com']
USERNAME = "efsdff"
PASSWORD = "sfdfsf"
# typical values for text_subtype are plain, html, xml
text_subtype = 'html'
content="""\
Test message number 2
"""
content=MICS.to_html()
subject="Sent from Python"
import sys
import os
import re
from smtplib import SMTP_SSL as SMTP # this invokes the secure SMTP protocol (port 465, uses SSL)
from email.mime.text import MIMEText
msg = MIMEText(content, text_subtype)
msg['Subject']= subject
msg['From'] = sender # some SMTP servers will do this automatically, not all
conn = SMTP(SMTPserver)
conn.set_debuglevel(False)
conn.starttls
conn.login(USERNAME, PASSWORD)
conn.sendmail(sender, destination, msg.as_string())