Change Unicode to Str returns “not supported”How do you change the size of figures drawn with matplotlib?How do I return multiple values from a function?What is the difference between encode/decode?Unicode (UTF-8) reading and writing to files in PythonConvert bytes to a string?Why does comparing strings using either '==' or 'is' sometimes produce a different result?Mixing unicode and str in python 2.X … problems?Pretty-print an entire Pandas Series / DataFramePython2&3 : compare str and unicodePython3 str(), bytes, and unicode
How would a family travel from Indiana to Texas in 1911?
What was the first multiprocessor x86 motherboard?
Where to pee in London?
Does the Voyager team use a wrapper (Fortran(77?) to Python) to transmit current commands?
WordCloud: do not eliminate duplicates
Unexpected route on a flight from USA to Europe
If there were no space agencies, could a person go to space?
Could one become a successful researcher by writing some really good papers while being outside academia?
Is there a loss of quality when converting RGB to HEX?
How do I say "Outdoor Pre-show"
Secure my password from unsafe servers
Was there ever a difference between 'volo' and 'volo'?
French equivalent of "Make leaps and bounds"
What can make Linux unresponsive for minutes when browsing certain websites?
How many years before enough atoms of your body are replaced to survive the sudden disappearance of the original body’s atoms?
Why should I "believe in" weak solutions to PDEs?
What does Fisher mean by this quote?
Double blind peer review when paper cites author's GitHub repo for code
Was Richard I's imprisonment by Leopold of Austria justified?
How do I get the =LEFT function in excel, to also take the number zero as the first number?
Need help understanding lens reach
Is it double speak?
What are these silver stripes on Cosmic Girl for?
What does VB stand for?
Change Unicode to Str returns “not supported”
How do you change the size of figures drawn with matplotlib?How do I return multiple values from a function?What is the difference between encode/decode?Unicode (UTF-8) reading and writing to files in PythonConvert bytes to a string?Why does comparing strings using either '==' or 'is' sometimes produce a different result?Mixing unicode and str in python 2.X … problems?Pretty-print an entire Pandas Series / DataFramePython2&3 : compare str and unicodePython3 str(), bytes, and unicode
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
In the program code is "unicode is not defined" returned. A change from unicode to str returns "str is not supported". What is wrong or missing?
for header in [ 'subject' ]:
dh = email.header.decode_header(msg[header])
default_charset = 'ASCII'
print('%-8s: %s' % (header.upper(), ''.join([ unicode(t[0], t[1] or default_charset) for t in dh ])))
python python-3.x email character-encoding decode
add a comment |
In the program code is "unicode is not defined" returned. A change from unicode to str returns "str is not supported". What is wrong or missing?
for header in [ 'subject' ]:
dh = email.header.decode_header(msg[header])
default_charset = 'ASCII'
print('%-8s: %s' % (header.upper(), ''.join([ unicode(t[0], t[1] or default_charset) for t in dh ])))
python python-3.x email character-encoding decode
add a comment |
In the program code is "unicode is not defined" returned. A change from unicode to str returns "str is not supported". What is wrong or missing?
for header in [ 'subject' ]:
dh = email.header.decode_header(msg[header])
default_charset = 'ASCII'
print('%-8s: %s' % (header.upper(), ''.join([ unicode(t[0], t[1] or default_charset) for t in dh ])))
python python-3.x email character-encoding decode
In the program code is "unicode is not defined" returned. A change from unicode to str returns "str is not supported". What is wrong or missing?
for header in [ 'subject' ]:
dh = email.header.decode_header(msg[header])
default_charset = 'ASCII'
print('%-8s: %s' % (header.upper(), ''.join([ unicode(t[0], t[1] or default_charset) for t in dh ])))
python python-3.x email character-encoding decode
python python-3.x email character-encoding decode
edited Mar 28 at 19:35
snakecharmerb
14.3k5 gold badges25 silver badges55 bronze badges
14.3k5 gold badges25 silver badges55 bronze badges
asked Mar 27 at 6:06
Josef SoeJosef Soe
225 bronze badges
225 bronze badges
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
The unicode builtin function does not exist in Python 3 - this is why you get the exception NameError: name 'unicode' is not defined
. In Python 3 the equivalent of unicode
is str.
Like unicode
, str
accepts an encoding argument and will try to decode a bytestring using the provided encoding. If you pass a str
instance to str
for decoding you will get TypeError: decoding str is not supported
.
The output of email.header.decode_header could include both str
and bytes
instances, so your comprehension needs to be able to handle both:
print('%-8s: %s' % ('subject'.upper(), ''.join(t[0] if isinstance(t[0], str) else str(t[0], t[1] or default_charset) for t in dh)))
(In Python 3 it's probably best to set default_charset to 'utf-8').
Finally, if you control how the message object is being created, you can have the headers automatically decoded by specifying a policy object when creating the message (Python 3.5+).
>>> from email.policy import default
>>> with open('message.eml', 'rb') as f:
... msg = email.message_from_bytes(f.read(), policy=default)
>>>
>>> for x in msg.raw_items():print(x)
...
('Subject', 'Ayons asperges pour le =?utf-8?q?d=C3=A9jeuner?=')
('From', '=?utf-8?q?Pep=C3=A9?= Le Pew <pepe@example.com>')
('To', 'Penelope Pussycat <penelope@example.com>,n Fabrette Pussycat <fabrette@example.com>')
('Content-Type', 'text/plain; charset="utf-8"')
('Content-Transfer-Encoding', 'quoted-printable')
('MIME-Version', '1.0')
>>> msg['from']
'Pepé Le Pew <pepe@example.com>'
>>> msg['subject']
'Ayons asperges pour le déjeuner'
(Message data taken from the email examples).
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55370780%2fchange-unicode-to-str-returns-not-supported%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The unicode builtin function does not exist in Python 3 - this is why you get the exception NameError: name 'unicode' is not defined
. In Python 3 the equivalent of unicode
is str.
Like unicode
, str
accepts an encoding argument and will try to decode a bytestring using the provided encoding. If you pass a str
instance to str
for decoding you will get TypeError: decoding str is not supported
.
The output of email.header.decode_header could include both str
and bytes
instances, so your comprehension needs to be able to handle both:
print('%-8s: %s' % ('subject'.upper(), ''.join(t[0] if isinstance(t[0], str) else str(t[0], t[1] or default_charset) for t in dh)))
(In Python 3 it's probably best to set default_charset to 'utf-8').
Finally, if you control how the message object is being created, you can have the headers automatically decoded by specifying a policy object when creating the message (Python 3.5+).
>>> from email.policy import default
>>> with open('message.eml', 'rb') as f:
... msg = email.message_from_bytes(f.read(), policy=default)
>>>
>>> for x in msg.raw_items():print(x)
...
('Subject', 'Ayons asperges pour le =?utf-8?q?d=C3=A9jeuner?=')
('From', '=?utf-8?q?Pep=C3=A9?= Le Pew <pepe@example.com>')
('To', 'Penelope Pussycat <penelope@example.com>,n Fabrette Pussycat <fabrette@example.com>')
('Content-Type', 'text/plain; charset="utf-8"')
('Content-Transfer-Encoding', 'quoted-printable')
('MIME-Version', '1.0')
>>> msg['from']
'Pepé Le Pew <pepe@example.com>'
>>> msg['subject']
'Ayons asperges pour le déjeuner'
(Message data taken from the email examples).
add a comment |
The unicode builtin function does not exist in Python 3 - this is why you get the exception NameError: name 'unicode' is not defined
. In Python 3 the equivalent of unicode
is str.
Like unicode
, str
accepts an encoding argument and will try to decode a bytestring using the provided encoding. If you pass a str
instance to str
for decoding you will get TypeError: decoding str is not supported
.
The output of email.header.decode_header could include both str
and bytes
instances, so your comprehension needs to be able to handle both:
print('%-8s: %s' % ('subject'.upper(), ''.join(t[0] if isinstance(t[0], str) else str(t[0], t[1] or default_charset) for t in dh)))
(In Python 3 it's probably best to set default_charset to 'utf-8').
Finally, if you control how the message object is being created, you can have the headers automatically decoded by specifying a policy object when creating the message (Python 3.5+).
>>> from email.policy import default
>>> with open('message.eml', 'rb') as f:
... msg = email.message_from_bytes(f.read(), policy=default)
>>>
>>> for x in msg.raw_items():print(x)
...
('Subject', 'Ayons asperges pour le =?utf-8?q?d=C3=A9jeuner?=')
('From', '=?utf-8?q?Pep=C3=A9?= Le Pew <pepe@example.com>')
('To', 'Penelope Pussycat <penelope@example.com>,n Fabrette Pussycat <fabrette@example.com>')
('Content-Type', 'text/plain; charset="utf-8"')
('Content-Transfer-Encoding', 'quoted-printable')
('MIME-Version', '1.0')
>>> msg['from']
'Pepé Le Pew <pepe@example.com>'
>>> msg['subject']
'Ayons asperges pour le déjeuner'
(Message data taken from the email examples).
add a comment |
The unicode builtin function does not exist in Python 3 - this is why you get the exception NameError: name 'unicode' is not defined
. In Python 3 the equivalent of unicode
is str.
Like unicode
, str
accepts an encoding argument and will try to decode a bytestring using the provided encoding. If you pass a str
instance to str
for decoding you will get TypeError: decoding str is not supported
.
The output of email.header.decode_header could include both str
and bytes
instances, so your comprehension needs to be able to handle both:
print('%-8s: %s' % ('subject'.upper(), ''.join(t[0] if isinstance(t[0], str) else str(t[0], t[1] or default_charset) for t in dh)))
(In Python 3 it's probably best to set default_charset to 'utf-8').
Finally, if you control how the message object is being created, you can have the headers automatically decoded by specifying a policy object when creating the message (Python 3.5+).
>>> from email.policy import default
>>> with open('message.eml', 'rb') as f:
... msg = email.message_from_bytes(f.read(), policy=default)
>>>
>>> for x in msg.raw_items():print(x)
...
('Subject', 'Ayons asperges pour le =?utf-8?q?d=C3=A9jeuner?=')
('From', '=?utf-8?q?Pep=C3=A9?= Le Pew <pepe@example.com>')
('To', 'Penelope Pussycat <penelope@example.com>,n Fabrette Pussycat <fabrette@example.com>')
('Content-Type', 'text/plain; charset="utf-8"')
('Content-Transfer-Encoding', 'quoted-printable')
('MIME-Version', '1.0')
>>> msg['from']
'Pepé Le Pew <pepe@example.com>'
>>> msg['subject']
'Ayons asperges pour le déjeuner'
(Message data taken from the email examples).
The unicode builtin function does not exist in Python 3 - this is why you get the exception NameError: name 'unicode' is not defined
. In Python 3 the equivalent of unicode
is str.
Like unicode
, str
accepts an encoding argument and will try to decode a bytestring using the provided encoding. If you pass a str
instance to str
for decoding you will get TypeError: decoding str is not supported
.
The output of email.header.decode_header could include both str
and bytes
instances, so your comprehension needs to be able to handle both:
print('%-8s: %s' % ('subject'.upper(), ''.join(t[0] if isinstance(t[0], str) else str(t[0], t[1] or default_charset) for t in dh)))
(In Python 3 it's probably best to set default_charset to 'utf-8').
Finally, if you control how the message object is being created, you can have the headers automatically decoded by specifying a policy object when creating the message (Python 3.5+).
>>> from email.policy import default
>>> with open('message.eml', 'rb') as f:
... msg = email.message_from_bytes(f.read(), policy=default)
>>>
>>> for x in msg.raw_items():print(x)
...
('Subject', 'Ayons asperges pour le =?utf-8?q?d=C3=A9jeuner?=')
('From', '=?utf-8?q?Pep=C3=A9?= Le Pew <pepe@example.com>')
('To', 'Penelope Pussycat <penelope@example.com>,n Fabrette Pussycat <fabrette@example.com>')
('Content-Type', 'text/plain; charset="utf-8"')
('Content-Transfer-Encoding', 'quoted-printable')
('MIME-Version', '1.0')
>>> msg['from']
'Pepé Le Pew <pepe@example.com>'
>>> msg['subject']
'Ayons asperges pour le déjeuner'
(Message data taken from the email examples).
answered Mar 28 at 19:34
snakecharmerbsnakecharmerb
14.3k5 gold badges25 silver badges55 bronze badges
14.3k5 gold badges25 silver badges55 bronze badges
add a comment |
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55370780%2fchange-unicode-to-str-returns-not-supported%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown