我无法使用 Python 将坐标导出到 shapefile。我的代码有问题还是模块有问题?
I cannot manage to export coordinates to a shapefile using Python. Is there a problem in my code or might the problem lie in the module?
这是我写的测试代码:
import shapefile
w = shapefile.Writer(shapefile.POLYGON)
w.poly(parts=[[[1,5],[5,5],[5,1],[3,3],[1,1]]])
w.field('FIRST_FLD','C','40')
w.field('SECOND_FLD','C','40')
w.record('First','Polygon')
w.save('shapefiles/test/polygon')
但是,它不起作用,我收到错误提示:
Traceback (most recent call last):
File "C:\src\Python\coordinates.py", line 33, in <module>
w = shapefile.Writer(shapefile.POINT) #shapefile.POLYGON)
File "C:\Python39\lib\site-packages\shapefile.py", line 1293, in __init__
self.shp = self.__getFileObj(os.path.splitext(target)[0] + '.shp')
File "C:\Python39\lib\ntpath.py", line 204, in splitext
p = os.fspath(p) TypeError: expected str, bytes or os.PathLike object, not int
好像是什么问题?我尝试使用其他参数,如“shapefile.POLYGON”,但同样的问题仍然存在。
shapefile.Writer()
期望文件名作为第一个参数,所以你的意思可能是:
w = shapefile.Writer('shapefiles/test/polygon')
你的最后一行应该改为
w.close()
这是我写的测试代码:
import shapefile
w = shapefile.Writer(shapefile.POLYGON)
w.poly(parts=[[[1,5],[5,5],[5,1],[3,3],[1,1]]])
w.field('FIRST_FLD','C','40')
w.field('SECOND_FLD','C','40')
w.record('First','Polygon')
w.save('shapefiles/test/polygon')
但是,它不起作用,我收到错误提示:
Traceback (most recent call last):
File "C:\src\Python\coordinates.py", line 33, in <module>
w = shapefile.Writer(shapefile.POINT) #shapefile.POLYGON)
File "C:\Python39\lib\site-packages\shapefile.py", line 1293, in __init__
self.shp = self.__getFileObj(os.path.splitext(target)[0] + '.shp')
File "C:\Python39\lib\ntpath.py", line 204, in splitext
p = os.fspath(p) TypeError: expected str, bytes or os.PathLike object, not int
好像是什么问题?我尝试使用其他参数,如“shapefile.POLYGON”,但同样的问题仍然存在。
shapefile.Writer()
期望文件名作为第一个参数,所以你的意思可能是:
w = shapefile.Writer('shapefiles/test/polygon')
你的最后一行应该改为
w.close()