简体   繁体   中英

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?

This is the test code I have written:

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')

However, it does not work and I am prompted with an error:

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

What seems to be the problem? I have tried to use other arguments like "shapefile.POLYGON" but the same problem remains.

shapefile.Writer() expects the filename as first parameter, so what you mean is probably:

w = shapefile.Writer('shapefiles/test/polygon')

and your last line should be instead

w.close()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM