I'm working with many shp file point format with geometry attributes like this:

enter image description here

But I need convert it into such format in textfile like below:

enter image description here

Are there any ways to arrange my shapefiles format into coordinates separated by comma orderly using PyQGIS or another python script.

Since I'm still learning with Python, I try to learning by doing.


2 Answers 2


If you want to write all the values of the ycoord field into a .txt file, you can execute the following code in the Python Console of QGIS (change the parameters with your own):

layer = iface.activeLayer() # your layer
features = layer.getFeatures() # features of your layer

with open('C:/Users/v.bre/Downloads/ycoord.txt','w') as my_file: # Change the path of the file
    for feature in features:
        my_file.write(str(feature["ycoord"]) + '\n') 

    my_file.close() # close the file

The result :

enter image description here

  • 2
    I think it's more : my_file.write(str(feature["ycoord"]) + "," + str(feature["xcoord"]) + ", "). Jan 17, 2020 at 14:30
  • Yes, I realized that afterwards, I proposed an alternative, I thought that he would adapt the code if necessary. The outline is there. Jan 17, 2020 at 14:33

You can use BulkVectorExport plug-in to save layers in your project to CSV (or to other output formats). In this case all columns are written to the text file, but you can use the Python sources of the plug-in to customize. Look for run function in bulkvectorexport.py, the QgsVectorFileWriter.writeAsVectorFormat function (in line 204) has an attributes parameter to select attributes to export (see the PyQGIS API Documentation).

There are some examples here.

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