3

In my attribute table there is a large number of fields. I want to export the data with selected fields.

How to do this in QGIS?

4
  • Are you asking how to select features and copy and paste them?
    – Erik
    Commented Mar 11, 2022 at 13:11
  • what kind of export do you expect to have ? you can also use the Save only selected features in Save Vector Layer as ...
    – Taras
    Commented Mar 11, 2022 at 13:26
  • 1
    @Taras I think they want to only save a selection of columns (fields) rather than features (rows).
    – Spacedman
    Commented Mar 11, 2022 at 13:39
  • @Spacedman, I can barely imagine this ... Besides the output format was not specified
    – Taras
    Commented Mar 11, 2022 at 13:43

2 Answers 2

13

Two ways:

  1. The "Save Vector Layer" dialog lets me choose which fields to save.

enter image description here

  1. Use either the "Drop Fields" or "Retain Fields" processing tools (in the "Vector Table" section of the processing toolbox) to create a new temporary scratch layer with a new set of fields. Then export that and delete it if you won't need it again.

You can probably set up a graphical model to do all this if you are going to be doing this a number of times.

1
  • its working!!!!!
    – Bruno B
    Commented Mar 14, 2022 at 4:45
4

I could suggest a PyQGIS way of implementing this task.

# referring to a needed layer
layer = iface.activeLayer()

# creating a temporal layer from selected features
temp_layer = layer.materialize(QgsFeatureRequest().setFilterFids(layer.selectedFeatureIds()))

# specifying important fields
needed_fields = ["id"]
all_fields = [field.name() for field in layer.fields()]
other_fields = list(set(all_fields) - set(needed_fields))

# getting indexes of other fields
idx = [temp_layer.fields().indexFromName(field) for field in other_fields]

# deleting other fields based on their indexes
temp_layer.dataProvider().deleteAttributes(idx)
temp_layer.updateFields()

# adding temporal output to the map canvas
QgsProject.instance().addMapLayer(temp_layer)

References:

2
  • 1
    A bit complicat for such a task!?!
    – katagena
    Commented Mar 11, 2022 at 15:55
  • @katagena seems like. I agree @_@
    – Taras
    Commented Mar 11, 2022 at 17:33

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.