我目前正在关注Joel Lawhead的Python地理空间分析教程书,我在尝试编辑shapefile时遇到了错误。
我正在使用的shapefile可用http://git.io/vLd8Y。我在python3上的Jupyter Notebook中运行我的代码。
这是我的代码。我只是在shapefile中读取一个Reader对象r
,并创建一个与w
具有相同形状类型的新Writer对象r
。然后,我试图将r
的记录附加到w
。
import shapefile
r = shapefile.Reader("NYC_MUSEUMS_GEO")
w = shapefile.Writer(r.shapeType)
w.fields = list(r.fields)
w.records.extend(r.records())
但是,我遇到了这个错误:
AttributeError Traceback (most recent call last)
<ipython-input-151-ceee096fbafa> in <module>()
6 w = shapefile.Writer(r.shapeType)
7 w.fields = list(r.fields)
----> 8 w.records.extend(r.records())
AttributeError: 'Writer' object has no attribute 'records'
有什么想法吗?
我无法重现您描述的问题。我开始时:
$ python
Python 3.5.4 (default, Oct 9 2017, 12:07:29)
>>>
并安装pyshp
模块:
$ pip install pyshp
...
Successfully installed pyshp-1.2.12
现在:
$ python
Python 2.7.13 (default, Dec 1 2017, 09:21:53)
[GCC 6.4.1 20170727 (Red Hat 6.4.1-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import shapefile
>>> r = shapefile.Reader("NYC_MUSEUMS_GEO")
>>> w = shapefile.Writer(r.shapeType)
>>> w.fields = list(r.fields)
>>> w.records.extend(r.records())
>>> len(w.records)
130
您使用的python
和pyshp
版本是否与我使用的相匹配?如果没有,您是否可以更新您的问题以包含您的环境的具体细节?