Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'encoding': 0.05; 'received:134': 0.05; 'binary': 0.07; 'parser': 0.07; 'sys': 0.07; 'string': 0.09; 'lookup': 0.09; 'msg': 0.09; 'subject:parsing': 0.09; 'python': 0.11; '58,': 0.16; 'charset': 0.16; 'fetch': 0.16; 'msg:': 0.16; 'to:name:python list': 0.16; 'typeerror:': 0.16; 'code.': 0.18; 'seems': 0.21; 'import': 0.22; 'header:User- Agent:1': 0.23; 'parse': 0.24; 'header': 0.24; 'push': 0.26; 'skip:" 30': 0.26; 'skip:" 40': 0.26; "doesn't": 0.30; 'converting': 0.30; 'gives': 0.31; 'trace': 0.31; 'file': 0.32; 'skip:# 10': 0.33; 'subject:from': 0.34; "can't": 0.35; 'convert': 0.35; 'data,': 0.36; 'feed': 0.38; 'to:addr:python-list': 0.38; 'rather': 0.38; 'to:addr:python.org': 0.39; 'read': 0.60; 'skip:* 10': 0.61; 'back': 0.62; 'pardon': 0.84; '8bit': 0.91; 'canonical': 0.91; 'mailbox.': 0.91 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Ap4EAIr3U1KGuA9I/2dsb2JhbABZgz/DKoNYQD0WGAMCAQIBPwwKAwgCiAKZGZgliRSNeQ+FZAOYAYEvhHOLXoMmgW8 Date: Tue, 08 Oct 2013 14:20:20 +0200 From: Antoon Pardon User-Agent: Mozilla/5.0 (X11; Linux i686; rv:17.0) Gecko/20130704 Icedove/17.0.7 MIME-Version: 1.0 To: Python List Subject: parsing email from stdin Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 44 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1381234806 news.xs4all.nl 15879 [2001:888:2000:d::a6]:44670 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:56389 I want to do some postprocessing on messages from a particular mailbox. So I use getmail which will fetch the messages and feed them to stdin of my program. As I don't know what encoding these messages will be in, I thought it would be prudent to read stdin as binary data. Using python 3.3 on a debian box I have the following code. #!/usr/bin/python3 import sys from email import message_from_file sys.stdin = sys.stdin.detach() msg = message_from_file(sys.stdin) which gives me the following trace back File "/home/apardon/.getmail/verdeler", line 7, in msg = message_from_file(sys.stdin) File "/usr/lib/python3.3/email/__init__.py", line 56, in message_from_file return Parser(*args, **kws).parse(fp) File "/usr/lib/python3.3/email/parser.py", line 58, in parse feedparser.feed(data) File "/usr/lib/python3.3/email/feedparser.py", line 167, in feed self._input.push(data) File "/usr/lib/python3.3/email/feedparser.py", line 100, in push data, self._partial = self._partial + data, '' TypeError: Can't convert 'bytes' object to str implicitly)) which seems to be rather odd. The following header are in the msg: Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit So why doesn't the email parser lookup the charset and use that for converting to string type? What is the canonical way to parse an email message from stdin? -- Antoon Pardon