1

I've been hammering at it for ages, but I just can't get pyshp to work. The CSV file looks like this:

lon,lat
-122.5163113, 45.55324148
-95.1719409, 29.97979461
153.0072888, -27.45988559
...

There are many more rows to be had. Using a lot of examples on the internet I've written the below code, but whenever I try to run it I get the error:

Traceback (most recent call last):
  File "H:/SU5050/shapefile.py", line 1, in <module>
    import shapefile
  File "H:/SU5050\shapefile.py", line 14, in <module>
    w=shapefile.writer(shapefile.POINT)
AttributeError: 'module' object has no attribute 'writer'

I've uninstalled and reinstalled pyshp (using pip) and when I try to simply import shapefile it doesn't error out, just when I try to run it.

import shapefile
import csv
#LON IS X (0)
#LAT IS Y (1)
file_location = 'H:\SU5050\WORK.csv'
out_file = 'H:\SU5050\GPS_Pts.shp'
x,y=[],[]
with open(file_location,'rb') as csvfile:
    r=csv.reader(csvfile,delimiter=',')
    for i,row in enumerate(r):
        if i>0:
            x.append(float(row[0]))
            y.append(float(row[1]))
w=shapefile.Writer(shapefile.POINT)
w.field('x','f',10,8)
w.field('y','f',10,8)
for j,k in enumerate(x):
    w.point(k,y[j])
    w.record(k,y[j])
    w.save(out_file)

Anyone have any idea what I'm doing wrong?

  • 1
    The error doesn't match. Is it really upper or lower case Writer() on line 14? Also, can you try w=shapefile.Writer(shapeType=1) instead? – BradHards Dec 3 '14 at 23:20
  • Brad is right. The code causing the error has a typo. The "w" in writer should be capitalized. The parameters shapefile.POINT and shapeType=1 are equivalent. The example you posted is correct. – GeospatialPython.com Dec 3 '14 at 23:36
  • I double checked and the w in Writer is capitalized in the code. Still getting the error message. Tried changing to shapeType=1 and got the same thing as well. – jaderust Dec 4 '14 at 0:57
  • I figured it out. This code was part of a much larger one that I pulled out to work on separately. As usual I named it before I started even coding and I named it shapefile.py. So when I was trying to import shapefile it was getting confused with the file name and popping out the error message. Renaming the file fixed it and the code is working as it was originally. – jaderust Dec 4 '14 at 1:43

Browse other questions tagged or ask your own question.