阅读电子邮件主体时无法编码字符
我在Apache Nifi中使用此代码来读取电子邮件主体(也在Stackoverflow上找到它):
import email
import mimetypes
from email.parser import Parser
from org.apache.commons.io import IOUtils
from java.nio.charset import StandardCharsets
from java.io import BufferedReader, InputStreamReader
from org.apache.nifi.processors.script import ExecuteScript
from org.apache.nifi.processor.io import InputStreamCallback
from org.apache.nifi.processor.io import StreamCallback
class PyInputStreamCallback(InputStreamCallback):
_text = None
def __init__(self):
pass
def getText(self) :
return self._text
def process(self, inputStream):
self._text = IOUtils.toString(inputStream, StandardCharsets.UTF_8)
flowFile = session.get()
if flowFile is not None :
reader = PyInputStreamCallback()
session.read(flowFile, reader)
msg = email.message_from_string(reader.getText())
body = ""
if msg.is_multipart():
for part in msg.walk():
ctype = part.get_content_type()
cdispo = str(part.get('Content-Disposition'))
if ctype == 'text/plain' and 'attachment' not in cdispo:
body = part.get_payload(decode=True) # decode
break
else:
body = msg.get_payload(decode=True)
flowFile = session.putAttribute(flowFile, 'msgbody', body.decode('utf-8', 'ignore'))
session.transfer(flowFile, ExecuteScript.REL_SUCCESS)
在大多数情况下它都可以,但是有时我会收到错误: unicodeCodeError:'ascii'编解码器无法编码字符u'\ u041a'在位置4784:ordinal不在范围内(128)
我从不使用python并尝试使用.encode修复它('utf-8')
在此行中:cdispo = str(part.get('content-disposition'))
,但是它没有帮助。也许我认为这个问题出现在这方面。
完整错误:
Processing failed: org.apache.nifi.processor.exception.ProcessException: javax.script.ScriptException: UnicodeEncodeError: 'ascii' codec can't encode character u'\u041a' in position 4784: ordinal not in range(128) in <script> at line number 30
- Caused by: javax.script.ScriptException: UnicodeEncodeError: 'ascii' codec can't encode character u'\u041a' in position 4784: ordinal not in range(128) in <script> at line number 30
- Caused by: Traceback (most recent call last):
File "<script>", line 30, in <module>
File "/opt/nifi/nifi-current/work/nar/extensions/nifi-scripting-nar-1.16.3.nar-unpacked/NAR-INF/bundled-dependencies/jython-standalone-2.7.2.jar/Lib/email/__init__$py.class", line 57, in message_from_string
File "/opt/nifi/nifi-current/work/nar/extensions/nifi-scripting-nar-1.16.3.nar-unpacked/NAR-INF/bundled-dependencies/jython-standalone-2.7.2.jar/Lib/email/parser$py.class", line 82, in parsestr
UnicodeEncodeError: 'ascii' codec can't encode character u'\u041a' in position 4784: ordinal not in range(128)
I'm using this code in Apache NiFi to read email body (found it on stackoverflow also):
import email
import mimetypes
from email.parser import Parser
from org.apache.commons.io import IOUtils
from java.nio.charset import StandardCharsets
from java.io import BufferedReader, InputStreamReader
from org.apache.nifi.processors.script import ExecuteScript
from org.apache.nifi.processor.io import InputStreamCallback
from org.apache.nifi.processor.io import StreamCallback
class PyInputStreamCallback(InputStreamCallback):
_text = None
def __init__(self):
pass
def getText(self) :
return self._text
def process(self, inputStream):
self._text = IOUtils.toString(inputStream, StandardCharsets.UTF_8)
flowFile = session.get()
if flowFile is not None :
reader = PyInputStreamCallback()
session.read(flowFile, reader)
msg = email.message_from_string(reader.getText())
body = ""
if msg.is_multipart():
for part in msg.walk():
ctype = part.get_content_type()
cdispo = str(part.get('Content-Disposition'))
if ctype == 'text/plain' and 'attachment' not in cdispo:
body = part.get_payload(decode=True) # decode
break
else:
body = msg.get_payload(decode=True)
flowFile = session.putAttribute(flowFile, 'msgbody', body.decode('utf-8', 'ignore'))
session.transfer(flowFile, ExecuteScript.REL_SUCCESS)
It works OK in most scenarios, but sometimes I receive an error:UnicodeEncodeError: 'ascii' codec can't encode character u'\u041a' in position 4784: ordinal not in range(128)
I never work with Python and tried to fix it with .encode('utf-8')
at this line: cdispo = str(part.get('Content-Disposition'))
, but it didn't help. Maybe I'm wrong with thought that problem is in this line.
Full error:
Processing failed: org.apache.nifi.processor.exception.ProcessException: javax.script.ScriptException: UnicodeEncodeError: 'ascii' codec can't encode character u'\u041a' in position 4784: ordinal not in range(128) in <script> at line number 30
- Caused by: javax.script.ScriptException: UnicodeEncodeError: 'ascii' codec can't encode character u'\u041a' in position 4784: ordinal not in range(128) in <script> at line number 30
- Caused by: Traceback (most recent call last):
File "<script>", line 30, in <module>
File "/opt/nifi/nifi-current/work/nar/extensions/nifi-scripting-nar-1.16.3.nar-unpacked/NAR-INF/bundled-dependencies/jython-standalone-2.7.2.jar/Lib/email/__init__$py.class", line 57, in message_from_string
File "/opt/nifi/nifi-current/work/nar/extensions/nifi-scripting-nar-1.16.3.nar-unpacked/NAR-INF/bundled-dependencies/jython-standalone-2.7.2.jar/Lib/email/parser$py.class", line 82, in parsestr
UnicodeEncodeError: 'ascii' codec can't encode character u'\u041a' in position 4784: ordinal not in range(128)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论