I'm approaching now for the first time to Python console and I wanted to know step by step how to export in .csv two fields from the attribute table of a vector shapefile(.shp) using just PyQgis. Let's say that up to now I have edited as follows:
fn = "file path"
vlayer = iface.addVectorLayer(fn, "file path", "ogr")
features = vlayer.getFeatures()
for feature in features:
print (feature [field], feature [2])
layer = QgsVectorLayer(fn, "ogr")
QgsVectorFileWriter.writeAsVectorFormat (layer, r "C: / path of file / filename_csv", "utf-8", None, "CSV", layerOptions = ['GEOMETRY = AS_XYZ']))
From this I get a .csv file but empty, how should I proceed?