Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed3.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'encoding': 0.05; 'output': 0.05; 'python3': 0.07; 'utf-8': 0.07; 'variables': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:process': 0.09; 'runs': 0.10; 'python': 0.11; '2.7': 0.14; "(i'm": 0.16; 'btw': 0.16; 'command,': 0.16; 'encodes': 0.16; 'fly': 0.16; 'lang': 0.16; 'latter,': 0.16; 'python),': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'seems': 0.21; 'command': 0.22; 'shell': 0.22; 'header:User-Agent:1': 0.23; '(such': 0.24; 'bytes': 0.24; 'parse': 0.24; 'skip:l 30': 0.24; 'fine': 0.24; 'environment': 0.24; 'question': 0.24; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'external': 0.29; 'skip:p 30': 0.29; 'wonder': 0.29; "i'm": 0.30; 'provided,': 0.31; 'run': 0.32; 'linux': 0.33; 'running': 0.33; 'guess': 0.33; 'case,': 0.35; 'module.': 0.36; 'thanks': 0.36; 'hi,': 0.36; 'should': 0.36; 'changing': 0.37; 'application': 0.37; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'changed': 0.39; 'received:org': 0.40; 'skip:u 10': 0.60; 'new': 0.61; 'here:': 0.62; 'relatively': 0.65; 'evaluate': 0.72; 'goal': 0.75; 'otten': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Francis Moreau Subject: Re: Decoding a process output Date: Wed, 05 Mar 2014 09:07:18 +0100 References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: au213-1-82-235-205-153.fbx.proxad.net User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.3.0 In-Reply-To: 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: 37 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1394006779 news.xs4all.nl 2926 [2001:888:2000:d::a6]:52934 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:67822 On 03/04/2014 05:05 PM, Peter Otten wrote: > Francis Moreau wrote: > >> Hi, >> >> In my understanding (I'm relatively new to python), I need to decode any >> bytes data provided, in my case, by a shell command (such as findmnt) >> started by the subprocess module. The goal of my application is to parse >> the command outputs. >> >> My application runs only on linux BTW and should run fine on both python >> 2.7 and py3k. >> >> My question is when decoding the output bytes data of the external >> command, which encoding should I use ? >> >> Should I guess the encoding by inspecting LANG or any LC_* environment >> variables ? >> >> Should I force one of those environment variable to a specific value >> before running my external command ? >> >> Thanks for any tips. > > You can use locale.getpreferredencoding(), which seems to evaluate LANG: > > $ python3 -c 'import locale; print(locale.getpreferredencoding())' > UTF-8 > $ LANG= python3 -c 'import locale; print(locale.getpreferredencoding())' > ANSI_X3.4-1968 Hmm I'm confused here: when unsetting or changing LANG, the encoding is changed on the fly that makes me wonder which part of the linux system encodes the command output. I don't think it's the latter, is the libc ? Thanks