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": qgis.utils.iface.setActiveLayer(layer) 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": qgis.utils.iface.setActiveLayer(layer) csv = qgis.utils.iface.activeLayer() # Set up join parameters shpField='LOC_CODE' csvField='LOC_CODE' joinObject = QgsVectorJoinInfo() joinObject.joinLayerId = csv.id() joinObject.joinFieldName = csvField joinObject.targetFieldName = shpField shp.addJoin(joinObject) # 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') shp.startEditing() 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]) shp.commitChanges() # Remove join shp.removeJoin(csv.LOC_CODE()) pass