PyQGIS – How to Export SHP Positions to CSV

csvpointpyqgisshapefile

I'm trying to export positions of a set of points using pyqgis

I saw the numerous posts concerning exporting shp dataset to csv but the command used in all of them is not working.

My code line is:

QgsVectorFileWriter.writeAsVectorFormat(final_palmtrees_layer, result_path+'_trees_pos.csv', "utf-8", None, "CSV", layerOptions ='GEOMETRY=AS_XY')

There are two issues with this line

  • I wrote it in the console and i'm getting a "7" error code in the console, but I cannot find what it means.
  • I put it in a script and i'm getting the csv file but only the attributes, not the points position + excel tells that the file is corrupted

Best Answer

Solved the issue by:

  • adding 'r' in front of the path: r'C:\...\xy.csv'
  • use a list and not a string as layerOptions parameter

Thanks Joseph