Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!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; 'python,': 0.02; 'url:pypi': 0.03; 'cpython': 0.05; 'encoding': 0.05; 'string.': 0.05; 'subject:text': 0.05; 'element': 0.07; 'encoded': 0.07; 'utf-8': 0.07; 'string': 0.09; 'method,': 0.09; 'subject:question': 0.10; '3.2.1': 0.16; 'encoding.': 0.16; 'guess.': 0.16; 'unicode,': 0.16; 'wrote:': 0.18; 'import': 0.22; 'header:User-Agent:1': 0.23; 'bytes': 0.24; 'query': 0.26; 'header :In-Reply-To:1': 0.27; 'installed': 0.27; 'record': 0.27; 'am,': 0.29; 'character': 0.29; '[2]': 0.30; 'characters': 0.30; 'code': 0.31; 'regular': 0.32; 'url:python': 0.33; 'running': 0.33; 'guess': 0.33; "i'd": 0.34; 'could': 0.34; 'but': 0.35; 'version': 0.36; 'library.': 0.36; 'ubuntu': 0.36; 'url:org': 0.36; 'should': 0.36; 'example,': 0.37; 'list': 0.37; 'thank': 0.38; 'to:addr :python-list': 0.38; 'anything': 0.39; 'does': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'first': 0.61; "you'll": 0.62; 'email addr:gmail.com': 0.63; 'information': 0.63; 'such': 0.63; 'different': 0.65; 'charset:windows-1252': 0.65; 'received:74.208': 0.68; 'presumably': 0.84; 'resto': 0.84 Date: Tue, 24 Feb 2015 06:25:24 -0500 From: Dave Angel User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.4.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Newbie question about text encoding References: In-Reply-To: Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Provags-ID: V02:K0:PMOrpaCP01c6tPUVzJGDou+c+cblJeLV9qOe71shgNy bLB83MEjYqG0yR66iOEsYiSOvGRbSq547QI6wS5/lHsX8cB637 6VKT+uONUpfmavccaUAnZfzrn+G6DOUGtuP7XWY5VSMw4MnL75 F8DXJ9KMHNPEBNg9GF4EweWhE1LgZlL1qRzFEPNWoHpWbmjYSW HLEnaYjVH3cPJl9LsB/u2GE3CKsvlS293Gr00fDKruebkju1V4 qfxDkdTJI7mahtWlkNtNzRroeJfmFBvKFohd3O7RIR5S8d7T4D TcaLtjvzGxcM8h4oEx2wqjX9J66/yCdIWlsDCxzyv6yQ9n2XRX MYSiPLsyZeYqjaFP9M5o= X-UI-Out-Filterresults: notjunk:1; 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: 61 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1424777143 news.xs4all.nl 2834 [2001:888:2000:d::a6]:57114 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:86316 On 02/24/2015 05:49 AM, pierrick.brihaye@gmail.com wrote: > Hello, > > Working with pyshp, this is my code : What version of Python, what version of pyshp, from where, and what OS? These are the first information to supply in any query that goes outside of the standard library. For example, you might be running CPython 3.2.1 on Ubuntu 14.04.1, and installed pyshp 1.2.1 from https://pypi.python.org/pypi/pyshp Or some other combination. > > 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 ? > > Thank you, > > p.b. > From my (cursory) reading of the pyshp docs on the above page, I cannot see what the [2] element of the record list should look like. So I'd have to guess. The bytes object is presumably an encoded version of the character string. I don't see anything on that page about unicode, or decode, so you might have to guess the encoding. Anyway, you can decode the bytestring into a regular string if you can correctly guess the encoding method, such as utf-8. If that were the right decoding, you could just use mystring = rec.decode() But utf-8 does not seem to be the right encoding for that bytestring. So you'll need a form like: mystring = rec.decode(encoding='xxx') for some value of xxx. -- DaveA