2

I am trying to follow the example at https://code.google.com/p/pyshp/wiki/ShapeRecords by entering the following code:

import shapefile
r = shapefile.Reader("ne_110m_admin_0_countries")
sr = r.shapeRecords()

However, it gives this error-message under Python 3,3 64 bit on Windows 7:

Traceback (most recent call last):
  File "D:\stuff\beispiel1.py", line 3, in <module>
    sr = r.shapeRecords()
  File "C:\Python33\lib\site-packages\shapefile.py", line 553, in shapeRecords
    for rec in zip(self.shapes(), self.records())]
  File "C:\Python33\lib\site-packages\shapefile.py", line 525, in records
    r = self.__record()
  File "C:\Python33\lib\site-packages\shapefile.py", line 501, in __record
    value = u(value)
  File "C:\Python33\lib\site-packages\shapefile.py", line 60, in u
    return v.decode('utf-8')
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xf4 in position 1: invalid continuation byte

What am I doing wrong?


Today I received an email from the developer of pyshp, Mr. Joel Lawhead. The problem I mentioned was a bug, which only occurs, when you use python vers 3.x . python vers. 2.x was not affected. He sent me a patched version, which works fine for me. He also told me, that this new version will be released soon.

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
Kurt
  • 7,087
  • 4
  • 33
  • 51

2 Answers2

2

Natural Earth data is Windows-1252 encoded, not UTF-8. I suggest seeing if changing the encoding from 'utf-8' to 'windows-1252' at line 60 in shapefile.py helps. If so, send a patch to the pyshp project.

sgillies
  • 9,056
  • 1
  • 33
  • 41
1

There is another way:

sf = shapefile.Reader(shp=shpFile, encoding='windows-1252')

Probably you'll need to change encoding to an appropriate one.

MrXsquared
  • 34,292
  • 21
  • 67
  • 117
OlegL
  • 11
  • 2