I have some layers that contain attachments (photos and videos). The layers have the following attributes:
['ATTACHMENTID', 'GLOBALID', 'REL_GLOBALID', 'CONTENT_TYPE', 'ATT_NAME', 'DATA_SIZE', 'DATA']
The attachments are stored in the DATA
field.
I want to export the attribute table as CSV except the DATA
field, as it inflates the field and contains no useful information.
I used the following code:
def save_layer_as_csv(layer, save_path, save_name):
'''Solutions from:https://gis.stackexchange.com/a/373856/95891
and:https://gis.stackexchange.com/a/348457/95891'''
QgsVectorFileWriter.writeAsVectorFormat(layer,f'{save_path}/{save_name}.csv',
"utf-8",driverName = "CSV" , layerOptions = ['GEOMETRY=AS_XYZ'])
This code exports all the fields which are not needed.
I found a good post here that exports only one field of the layer by adding attributes=[idx], onlySelected=True
to the line above, but I don't know how to slice the fields as object and add them to the attribute
parameter!
I tried to slice using the layer.fields()[:-1]
code, but it returned an error argument 1 has unexpected type 'slice'
How can I solve this issue to export all the fields except the last one?
Note: I use QGIS 3.26.2-Buenos Aires