The str object does not have the decode attribute. Python 3 error?

Here is my code:

import imaplib from email.parser import HeaderParser conn = imaplib.IMAP4_SSL('imap.gmail.com') conn.login('example@gmail.com', 'password') conn.select() conn.search(None, 'ALL') data = conn.fetch('1', '(BODY[HEADER])') header_data = data[1][0][1].decode('utf-8') 

at this moment i get an error

 AttributeError: 'str' object has no attribute 'decode' 

Python 3 no longer has decryption, right? How can i fix this?

Also in:

 data = conn.fetch('1', '(BODY[HEADER])') 

I choose only the first letter. How to choose everything?

+138
python imaplib
Feb 18 '15 at 12:20
source share
6 answers

You are trying to decode an object that is already decoded. You have str , no need to decrypt it from UTF-8.

Just release the .decode('utf-8') :

 header_data = data[1][0][1] 

As for your call to fetch() , you are explicitly requesting only the first message. Use a range if you want more messages. See Documentation:

The message_set parameters for the commands below is a string indicating one or more messages to be received. This can be a simple message number ( '1' ), a range of message numbers ( '2:4' ), or a group of non-contiguous ranges separated by commas ( '1:3,6:9' ). The range may contain an asterisk indicating an infinite upper bound ( '3:*' ).

+145
Feb 18 '15 at 12:41
source share

Start with Python 3, the whole line will be a unicode object.

  a = 'Happy New Year' # Python 3 b = unicode('Happy New Year') # Python 2 

the code is the same before. So I think you should remove .decode('utf-8') . Since you already got a unicode object.

+29
Feb 18 '15 at 12:44
source share

Use it with this method:

 str.encode().decode() 
+24
Mar 27 '17 at 23:08
source share

I am not familiar with the library, but if your problem is that you do not need an array of bytes, one simple way is to specify the encoding type directly in the broadcast:

 >>> my_byte_str b'Hello World' >>> str(my_byte_str, 'utf-8') 'Hello World' 
+6
Nov 22 '17 at 4:26
source share

It is already decrypted in Python3, try it directly, it should work.

+3
Jul 11 '18 at 5:33
source share

For Python3

 html = """\\u003Cdiv id=\\u0022contenedor\\u0022\\u003E \\u003Ch2 class=\\u0022text-left mb-2\\u0022\\u003EInformaci\\u00f3n del veh\\u00edculo de patente AA345AA\\u003C\\/h2\\u003E\\n\\n\\n\\n \\u003Cdiv class=\\u0022panel panel-default panel-disabled mb-2\\u0022\\u003E\\n \\u003Cdiv class=\\u0022panel-body\\u0022\\u003E\\n \\u003Ch2 class=\\u0022table_title mb-2\\u0022\\u003EInformaci\\u00f3n del Registro Automotor\\u003C\\/h2\\u003E\\n \\u003Cdiv class=\\u0022col-md-6\\u0022\\u003E\\n \\u003Clabel class=\\u0022control-label\\u0022\\u003ERegistro Seccional\\u003C\\/label\\u003E\\n \\u003Cp\\u003ESAN MIGUEL N\\u00b0 1\\u003C\\/p\\u003E\\n \\u003Clabel class=\\u0022control-label\\u0022\\u003EDirecci\\u00f3n\\u003C\\/label\\u003E\\n \\u003Cp\\u003EMAESTRO ANGEL D\\u0027ELIA 766\\u003C\\/p\\u003E\\n \\u003Clabel class=\\u0022control-label\\u0022\\u003EPiso\\u003C\\/label\\u003E\\n \\u003Cp\\u003EPB\\u003C\\/p\\u003E\\n \\u003Clabel class=\\u0022control-label\\u0022\\u003EDepartamento\\u003C\\/label\\u003E\\n \\u003Cp\\u003E-\\u003C\\/p\\u003E\\n \\u003Clabel class=\\u0022control-label\\u0022\\u003EC\\u00f3digo postal\\u003C\\/label\\u003E\\n \\u003Cp\\u003E1663\\u003C\\/p\\u003E\\n \\u003C\\/div\\u003E\\n \\u003Cdiv class=\\u0022col-md-6\\u0022\\u003E\\n \\u003Clabel class=\\u0022control-label\\u0022\\u003ELocalidad\\u003C\\/label\\u003E\\n \\u003Cp\\u003ESAN MIGUEL\\u003C\\/p\\u003E\\n \\u003Clabel class=\\u0022control-label\\u0022\\u003EProvincia\\u003C\\/label\\u003E\\n \\u003Cp\\u003EBUENOS AIRES\\u003C\\/p\\u003E\\n \\u003Clabel class=\\u0022control-label\\u0022\\u003ETel\\u00e9fono\\u003C\\/label\\u003E\\n \\u003Cp\\u003E(11)46646647\\u003C\\/p\\u003E\\n \\u003Clabel class=\\u0022control-label\\u0022\\u003EHorario\\u003C\\/label\\u003E\\n \\u003Cp\\u003E08:30 a 12:30\\u003C\\/p\\u003E\\n \\u003C\\/div\\u003E\\n \\u003C\\/div\\u003E\\n\\u003C\\/div\\u003E \\n\\n\\u003Cp class=\\u0022text-center mt-3 mb-1 hidden-print\\u0022\\u003E\\n \\u003Ca href=\\u0022javascript:window.print();\\u0022 class=\\u0022btn btn-default\\u0022\\u003EImprim\\u00ed la consulta\\u003C\\/a\\u003E \\u0026nbsp; \\u0026nbsp;\\n \\u003Ca href=\\u0022\\u0022 class=\\u0022btn use-ajax btn-primary\\u0022\\u003EHacer otra consulta\\u003C\\/a\\u003E\\n\\u003C\\/p\\u003E\\n\\u003C\\/div\\u003E""" 
 print(html.replace("\\/", "/").encode().decode('unicode_escape')) 
+3
Jul 24 '19 at 19:11
source share



All Articles