3

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
5
  • Which QGIS version and platform are you using?
    – Joseph
    Apr 9, 2018 at 10:17
  • QGIS 2.18.14 on windows 10
    – Clement
    Apr 9, 2018 at 10:34
  • Try using layerOptions as a list. E.g. layerOptions=['GEOMETRY=AS_XY']
    – Joseph
    Apr 9, 2018 at 10:36
  • I now get error code 3 with QgsVectorFileWriter.writeAsVectorFormat(layer, 'C:\...\xy.csv', "utf-8", None, "CSV", layerOptions =['GEOMETRY=AS_XY']). Indeed it's written list in QGIS API, I just saw it. Is there a place to see error codes ?
    – Clement
    Apr 9, 2018 at 11:32
  • https://qgis.org/api/classQgsVectorFileWriter.html#a3a4405a59d8f8ac147878cae5bd9bade. Also, be careful with backward slashes. Use r'C:\...\xy.csv' or 'C:\...\\xy.csv'
    – Joseph
    Apr 9, 2018 at 11:45

1 Answer 1

3

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.