Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed2a.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.022 X-Spam-Evidence: '*H*': 0.96; '*S*': 0.00; 'needed,': 0.07; 'utf-8': 0.07; 'string': 0.09; 'broke': 0.09; 'subject:skip:c 10': 0.09; '(it': 0.16; '23,': 0.16; 'non-ascii': 0.16; 'rewriting': 0.16; 'unicode,': 0.16; 'received:74.125.82.44': 0.16; 'wrote:': 0.18; 'skip:f 30': 0.19; 'email addr:gmail.com>': 0.22; 'to:name :python-list@python.org': 0.22; 'header:In-Reply-To:1': 0.27; 'subject:list': 0.30; 'message-id:@mail.gmail.com': 0.30; 'url:mailman': 0.30; 'code': 0.31; 'fine,': 0.31; 'larry': 0.31; 'sep': 0.31; 'option': 0.32; 'url:python': 0.33; 'received:74.125.82': 0.34; 'could': 0.34; 'subject:with': 0.35; 'except': 0.35; 'convert': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'subject:data': 0.36; 'url:listinfo': 0.36; 'url:org': 0.36; 'list': 0.37; 'skip:[ 10': 0.38; 'to:addr:python- list': 0.38; 'pm,': 0.38; 'received:74.125': 0.39; 'skip:. 10': 0.39; 'to:addr:python.org': 0.39; 'changed': 0.39; 'url:mail': 0.40; 'how': 0.40; 'skip:\xc2 10': 0.60; 'today': 0.64; '8bit%:10': 0.64; 'end.': 0.84; 'needed:': 0.84; 'subject:Best': 0.91 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:content-type; bh=eP5i5M/ZWHkNLJ/3BVRPHs9zDMw16pJxxuFyKv9fxWQ=; b=CGqJMkBtAr3zsWCYCFlEa3hFVNtm5gg9beQrggYSTNjsQqRsa6V5QsjF0AxMcEIBtE iOcxzsG+lxtLmhhuoVdfjvZ99Nx/33vXOWAxZGAFv1kGBhb0cAbYiPkqslLCyasf8+Ah r+xvd/giiISDaIWSkaFJulAAlCkkH0U9+ECBC4hILv8Ie3zZadkpvrOucgF8aiJDAdOa 40bRiFbOwXoJqSFooMQuRnyZXSvEPiLA05gZUy3XXeJJgExe2Z+O5a1+VmU+V1/boe22 svlR62LtEuE84onjudqBc5+nD+P9uapEViGDe3B7/lLRqwgSdgn/4SHpubatfarrwsKp 3NIg== X-Gm-Message-State: ALoCoQney8eXspAP0O2tcpnJBIamoJJNky6YpbJz++JOxXSuDGYmpCZBEnFLxgr2cR/uzcogkYbl X-Received: by 10.180.99.195 with SMTP id es3mr25562158wib.33.1411510662176; Tue, 23 Sep 2014 15:17:42 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: References: From: Chris Kaynor Date: Tue, 23 Sep 2014 15:17:22 -0700 Subject: Re: Best way to deal with different data types in a list comprehension To: "python-list@python.org" Content-Type: multipart/alternative; boundary=f46d04426f9c9dba890503c2ef87 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: 84 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1411510664 news.xs4all.nl 2833 [2001:888:2000:d::a6]:53382 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:78256 --f46d04426f9c9dba890503c2ef87 Content-Type: text/plain; charset=UTF-8 On Tue, Sep 23, 2014 at 3:01 PM, Larry Martell wrote: > I have some code that I inherited: > > ' '.join([self.get_abbrev()] + > [str(f['value') > for f in self.filters > if f.has_key('value')]).strip() > > This broke today when it encountered some non-ascii data. > One option would be to do the processing in unicode, and convert to utf-8 only when needed: u' '.join([self.get_abbrev()] + [unicode(f['value') for f in self.filters if f.has_key('value')]).strip() If needed, add a .encode('utf-8') to the end. > > I changed the str(f['value']) line to f['value'].encode('utf-8'), > which works fine, except when f['value'] is not a string (it could be > anything). > > Without rewriting this without the list comprehension, how can I write > this to deal with both strings and non-strings? > -- > https://mail.python.org/mailman/listinfo/python-list > --f46d04426f9c9dba890503c2ef87 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable

--f46d04426f9c9dba890503c2ef87--