通过新的Writer对象在Python中编辑shapefile无效

问题描述 投票:0回答:1

我目前正在关注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 gis shapefile
1个回答
0
投票

我无法重现您描述的问题。我开始时:

$ 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

您使用的pythonpyshp版本是否与我使用的相匹配?如果没有,您是否可以更新您的问题以包含您的环境的具体细节?

© www.soinside.com 2019 - 2024. All rights reserved.