I am following the tutorial here for how to convert a csv to shp using pyshp. I am almost there, with one issue. In the tutorial, the person loops over the rows in the csv, and for each field, assigns the value of that field to a variable (shortened example):
import shapefile # import pyshp library trees_shp = shapefile.Writer(shapefile.POINT) for row in reader: tree_id = row address = row town = row .... condition = row latitude = row longitude = row
They then record the new point with attributes like this:
trees_shp.point(float(longitude),float(latitude)) trees_shp.record(tree_id, address, town, ..., condition)
My question is, is there another (better) way to record field values that are stored in a list (or comma separated string)? i.e. I am looking to record the attributes that are stored in 2 variables (for each row/point):
row_str = 'fieldValue1, fieldValue2, fieldValue3' row_list = ['fieldValue1', 'fieldValue2', 'fieldValue3']
I would like to just do something like:
outShpWriter = shapefile.Writer(shapefile.POINT) outShpWriter.record(row_str) # or outShpWriter.record(row_list)
but each gives me the following error:
File "/usr/local/lib/python2.7/dist-packages/shapefile.py", line 1071, in record record = [recordList[i] for i in range(fieldCount)] IndexError: tuple index out of range
Is there a workaround besides having to explicitly define variables i.e. (f1 = row_list) and record those variables to the shp?
I ask because the number of fields/fields themselves may change and I would like to automate as much as possible to make it flexible.