processing.algorithmHelp('qgis:zonalstatistics') prints out:


INPUT_VECTOR:  <QgsProcessingOutputVectorLayer>
        Zonal statistics

but I'd like to redirect the output to a CSV file with only the id column of the INPUT_VECTOR and the output of the Zonal statistics algorithm. Is this possible??

Is there some way to redirect the output?

1 Answer 1


Unless you modify the source code for the script, it will just save the attributes to the input vector file. A possible alternative could be to run the algorithm and then export the input layer as a csv file with only the fields you're interested in:

layer = iface.activeLayer()
output_path = 'path/to/output.csv'
zone_stats = ("id", "Count", "Sum", "Mean")
field_list = [layer.fields().indexFromName(fields.name()) for fields in layer.fields() if any(s in fields.name() for s in tuple(zone_stats))]
QgsVectorFileWriter.writeAsVectorFormat(layer, output_path, "UTF-8", driverName="CSV", attributes=field_list)

Your Answer

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

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