Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python TypeError:需要类似字节的对象,而不是';str';但类型显示字节_Python_Python 3.x_String_Flask_Byte - Fatal编程技术网

Python TypeError:需要类似字节的对象,而不是';str';但类型显示字节

Python TypeError:需要类似字节的对象,而不是';str';但类型显示字节,python,python-3.x,string,flask,byte,Python,Python 3.x,String,Flask,Byte,此代码的输出: print(type(body)) body = body.replace('\n', '<br>') 以下是完整的代码片段,供参考,我试图在网页上以html显示电子邮件内容: def GetMimeMessage(service, user_id, msg_id): try: message = service.users().messages().get(userId=user_id, id=msg_id, format='raw').execut

此代码的输出:

print(type(body))
body = body.replace('\n', '<br>')
以下是完整的代码片段,供参考,我试图在网页上以html显示电子邮件内容:

def GetMimeMessage(service, user_id, msg_id):

  try:
    message = service.users().messages().get(userId=user_id, id=msg_id, format='raw').execute()
    msg_bytes = base64.urlsafe_b64decode(message['raw'].encode('ASCII'))
    b = email.message_from_bytes(msg_bytes)
    body = ""

    if b.is_multipart():
      for part in b.walk():
        ctype = part.get_content_type()
        cdispo = str(part.get('Content-Disposition'))

    # skip any text/plain (txt) attachments
    if ctype == 'text/plain' and 'attachment' not in cdispo:
      body = part.get_payload(decode=True)  # decode
      break
    # not multipart - i.e. plain text, no attachments, keeping fingers crossed
    else:
      body = b.get_payload(decode=True)

    print(type(body))
    body = body.replace('\n', b'<br>')
    return body
  except errors.HttpError as error:
    print ('An error occurred: %s' % error)
def GetMimeMessage(服务、用户id、消息id):
尝试:
message=service.users().messages().get(userId=user\u id,id=msg\u id,format='raw')。execute()
msg_bytes=base64.urlsafe_b64解码(消息['raw'].encode('ASCII'))
b=电子邮件。来自字节的消息(消息字节)
body=“”
如果b.是_multipart():
对于b.walk()中的部分:
ctype=part.get\u content\u type()
cdispo=str(part.get('Content-Disposition'))
#跳过任何文本/纯(txt)附件
如果ctype==“文本/普通”和“附件”不在cdispo中:
body=part.get_有效载荷(decode=True)#decode
打破
#非多部分-即纯文本,无附件,祈祷好运
其他:
body=b.get_有效载荷(decode=True)
打印(类型(正文))
body=body.replace('\n',b'
')) 返回体 除了errors.HttpError作为错误: 打印('发生错误:%s'%1错误)
更改此选项

body = body.replace('\n', b'<br>')

你能做
打印(正文)
并公布结果吗?你的参数也必须是
字节
对象。因此,您使用
body.replace('\n','
')
而不是
body.replace(b'\n',b'
')
@juanpa.arrivillaga OP尝试了该操作,但无效。在文章中提到。由于某种原因,
打印(正文)
语句显示一条语句是
。我添加了一个if语句来检查类型:
if类型(body)=字节:
body=body.decode()
def GetMimeMessage(service, user_id, msg_id):

  try:
    message = service.users().messages().get(userId=user_id, id=msg_id, format='raw').execute()
    msg_bytes = base64.urlsafe_b64decode(message['raw'].encode('ASCII'))
    b = email.message_from_bytes(msg_bytes)
    body = ""

    if b.is_multipart():
      for part in b.walk():
        ctype = part.get_content_type()
        cdispo = str(part.get('Content-Disposition'))

    # skip any text/plain (txt) attachments
    if ctype == 'text/plain' and 'attachment' not in cdispo:
      body = part.get_payload(decode=True)  # decode
      break
    # not multipart - i.e. plain text, no attachments, keeping fingers crossed
    else:
      body = b.get_payload(decode=True)

    print(type(body))
    body = body.replace('\n', b'<br>')
    return body
  except errors.HttpError as error:
    print ('An error occurred: %s' % error)
body = body.replace('\n', b'<br>')
body = body.decode()
body = body.replace('\n', '<br>')
>>> s = b'asdf\nasdfa\n'
>>> s
b'asdf\nasdfa\n'
>>> s.replace('\n','<br>')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: a bytes-like object is required, not 'str'
>>> s.decode().replace('\n','<br>')
'asdf<br>asdfa<br>'