QGIS has a built-in Python console, where you can enter commands in the Python programming language and get results. This is very useful for quick data processing.
To follow this recipe, you should be familiar with the Python programming language. You can find a small but detailed tutorial in the official Python documentation at https://docs.python.org/2.7/tutorial/index.html.
Also load the poi_names_wake.shp file from the sample data.
QGIS Python console can be opened by clicking on the Python Console button at toolbar or by navigating to Plugins | Python Console. The console opens as a non-modal floating window, as shown in the following screenshot:

Let's take a look at how to perform some data exploration with the QGIS Python console:
layer = iface.activeLayer()
layer.featureCount()
for f in layer.getFeatures(): print f["featurenam"], f["elev_m"]
You can also use the Python console for more complex tasks, such as exporting features with some attributes to a text file. Here is how to do this:
import csv
layer = iface.activeLayer()
with open('c:\\temp\\export.csv', 'wb') as outputFile:
writer = csv.writer(outputFile)
for feature in layer.getFeatures():
geom = feature.geometry().exportToWkt()
writer.writerow([geom, feature["featurenam"], feature['elev_m"]])poi_names_wake.shp, which is provided with this book, adjust attribute names in line 8.In line 1, we imported the csv module from the standard Python library. This module provides a convenient way to read and write comma-separated files. In line 3, we obtained a reference to the currently selected layer, which will be used later to access layer features.
In line 3, an output file opened. Note that here we use the with statement so that later there is no need to close the file explicitly, context manager will do this work for us. In line 5, we set up the so-called writer—an object that will write data to the CSV file using specified format settings.
In line 6, we started iterating over features of the active layer. For each feature, we extracted its geometry and converted it into a Well-Known Text (WKT) format (line 7). We then wrote this text representation of the feature geometry with some attributes to the output file (line 8).
It is necessary to mention that our script is very simple and will work only with attributes that have ASCII encoding. To handle non-Latin characters, it is necessary to convert the output data to the unicode before writing it to file.
Using the Python console, you also can invoke Processing algorithms to create complex scripts for automated analysis and/or data preparation.
To make the Python console even more useful, take a look at the Script Runner plugin. Detailed information about this plugin with some usage examples can be found at http://spatialgalaxy.net/2012/01/29/script-runner-a-plugin-to-run-python-scripts-in-qgis/.