Path: csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed1.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.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'encoding': 0.05; 'subject:text': 0.05; 'that?': 0.05; 'string': 0.09; 'subject:question': 0.10; 'cc:addr:python-list': 0.11; 'windows': 0.15; '24,': 0.16; 'expecting': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'objects.': 0.16; 'there?': 0.16; 'wrote:': 0.18; 'first.': 0.19; 'help.': 0.21; 'feb': 0.22; 'import': 0.22; 'cc:addr:python.org': 0.22; 'bytes': 0.24; 'string,': 0.24; 'question': 0.24; 'cc:2**0': 0.24; 'header:In- Reply-To:1': 0.27; 'character': 0.29; 'characters': 0.30; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; 'figure': 0.32; 'guess': 0.33; 'raw': 0.33; "can't": 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'doing': 0.36; 'turn': 0.37; 'list': 0.37; 'whatever': 0.38; 'pm,': 0.38; 'that,': 0.38; 'mailing': 0.39; 'how': 0.40; "you're": 0.61; 'back': 0.62; 'different': 0.65; '2015': 0.84; 'resto': 0.84; 'to:none': 0.92 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=YPu1Ih0Nsk3iDniHAZ1Ay9FwwFfQ7YuRzSt/MZ34T3Y=; b=vyQhquy3AY4SR61q/8a7Idss/RYGsUU2N0sIT0zvYEjNfb9hvHWvWOnk+nTTcIRqa0 W4YjeO+LNXy49cnKDKSEwINKL+on+4IdQp/3bRvqOzD4sY3ndTeiFzzVanMqsjaMaq78 gb3TKDfKQWKJm6uxSeGtjdge8pVgMar2mZ8vacPHvGQznhjdr4Dc/vhRU05ZW49pJAmz tDtUzZYuKBAupSecmk40g0lYfKIMlrl691e7t+84uevKQgAdIToMQ1Y/9WSvy+OeHfOb lPtSoy3klr1CYVEuU8EAcGBRq90RnrBIvQschdYiaf68irKogMRUmo37HzRKgEenJFze I2sw== MIME-Version: 1.0 X-Received: by 10.107.128.219 with SMTP id k88mr19631554ioi.27.1424776170925; Tue, 24 Feb 2015 03:09:30 -0800 (PST) In-Reply-To: References: Date: Tue, 24 Feb 2015 22:09:30 +1100 Subject: Re: Newbie question about text encoding From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 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: 30 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1424776180 news.xs4all.nl 2833 [2001:888:2000:d::a6]:48958 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:86314 On Tue, Feb 24, 2015 at 9:49 PM, wrote: > Working with pyshp, this is my code : > > import shapefile > > inFile = shapefile.Reader("blah") > > for sr in inFile.shapeRecords(): > rec = sr.record[2] > print("Output : ", rec, type(rec)) > > Output: hippodrome du resto > Output: b'stade de man\xe9 braz' > > Why do I get 2 different types ? > How to get a string object when I have accented characters ? I don't know what pyshp is doing here, so you may want to seek a pyshp-specific mailing list for help. My guess is that it's automatically decoding to str if it's ASCII-only, and giving you back the raw bytes if there are any that it can't handle. The question is: What encoding _is_ that? Do you know what character you're expecting to see there? Before you can turn that into a string, you have to figure out whether it's Latin-1 (ISO-8859-1), or some other ISO-8859-x standard, or a Windows codepage, or an ancient thing off a Mac, or whatever else it might be. Once you know that, it's easy: you just decode() the bytes objects. But you MUST figure out the encoding first. ChrisA