Iт QGIS 3.10 I have imported a CSV which contains a column of numeric data (let's call this attribute
"x"). However, it is imported automatically as strings. I can easily create a new field by clicking the Field Calculator and typing
Because I need to repeat this many times, I would like to use PyQGIS to do this.
I've been trying various ways but I still can't get it to work. For example, I was following this: Struggling to use Field Calculator - PyQGIS
Could someone please tell me what I did wrong?
#import data csv_file = QgsVectorLayer(path_to_csv, 'nameoflayer') QgsProject.instance().addMapLayer(csv_file) csv = QgsProject.instance().mapLayersByName('nameoflayer') #start editing this layer with edit(csv): for feature in selected_feature: feature.setAttribute(feature.fieldNameIndex('newfieldname'), to_real(feature['x'])) updateFeature(feature)
There is no error message but I can't see the new column called
"newfieldname" when I open the attribute table after running the above code.