I know this has been discussed before, and I used the code provided by @Joseph in How to join data from excel to an attribute table in QGIS without creating duplicates for my Macros.

I am trying to achieve what @Joseph said, but I am attaching a screenshot of the error that I got when I did what you said. Below is my complete macros code.

'Noviembre_P01' is my shapefile 'project_2_gis_mapping.csv' is the csv file that I want to be able to connect automatically to this shapefile so that whenever my csv is updated, the attribute table in the shapefile also updates itself. 'LOC_CODE' is the common attribute to my .shp and .csv files. 'first_bloo' and 'diagnosis' are the 2 fields I am interested in mainly, out of all the fields in the csv file.

I also did not create a join like he mentioned.

Conversely, will using 'Watch File' in the 'Add Delimited Text Layer' dialogue box server the exact same purpose?

def openProject(): from PyQt4.QtCore import *
import qgis
from qgis.core import QgsMapLayerRegistry, QgsVectorJoinInfo

for layer in QgsMapLayerRegistry.instance().mapLayers().values():
    # Change to your shapefile name
    if layer.name() == "Noviembre_P01":
        shp = qgis.utils.iface.activeLayer()

for layer in QgsMapLayerRegistry.instance().mapLayers().values():
    # Change to your csv name
    if layer.name() == "project_2_gis_mapping.csv":
        csv = qgis.utils.iface.activeLayer()

# Set up join parameters
joinObject = QgsVectorJoinInfo()
joinObject.joinLayerId = csv.id()
joinObject.joinFieldName = csvField
joinObject.targetFieldName = shpField

# Define fields to update and joined fields to copy values from
ip1 = shp.fieldNameIndex('first_bloo') 
ip1_join = shp.fieldNameIndex('project_2_gis_mapping.csv_first_bloo')
ip2 = shp.fieldNameIndex('diagnosis') 
ip2_join = shp.fieldNameIndex('project_2_gis_mapping.csv_diagnosis')

for feat in shp.getFeatures():
    shp.changeAttributeValue(feat.id(), first_bloo, feat.attributes()[first_bloo_join])
    shp.changeAttributeValue(feat.id(), diagnosis, feat.attributes()[diagnosis_join])

# Remove join 

Screenshot of Error Message

1 Answer 1


The problem is with the last line of code: pass. You can simply remove it as it is not needed in this situation (it is mainly useful with empty functions).

And to explain the error, Python expects all code to be properly indented. The faulty last line is indented more than the previous line and it is not part of a logical group.

  • Thanks for replying; I removed the last line, but I am still getting an error. I don't know how to attach the screenshot of that here, but it says " NameError: name 'csv' is not defined". I cross-checked the name of my csv file and it is the same as that previously mentioned in my Macros script. Thanks! Jul 8, 2017 at 20:41
  • Please edit your post and paste the complete error message. It contains useful info, such as the line at which the error occurs
    – JGH
    Jul 8, 2017 at 20:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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