Skip to main content
thousand separator
Source Link
J. Monticolo
  • 15.9k
  • 1
  • 29
  • 65

Is this code faster (tested on a 300 features memory layer) :

layer = iface.activeLayer()

# Get name and type of attributes in layer
intfieldsnames = [i.name() for i in layer.fields() if i.type() in [QVariant.LongLong, QVariant.Int]]

with edit(layer):
    prov = layer.dataProvider()
    new_fields = [QgsField(f"{a}d", QVariant.String) for a in intfieldsnames]
    prov.addAttributes(new_fields)
    layer.updateFields()

    for feat in layer.getFeatures():
        for field_name in intfieldsnames:
            feat[f"{field_name}d"] = str(f"{feat[field_name]:,}".replace(",", ".")

        layer.updateFeature(feat)

Is this code faster (tested on a 300 features memory layer) :

layer = iface.activeLayer()

# Get name and type of attributes in layer
intfieldsnames = [i.name() for i in layer.fields() if i.type() in [QVariant.LongLong, QVariant.Int]]

with edit(layer):
    prov = layer.dataProvider()
    new_fields = [QgsField(f"{a}d", QVariant.String) for a in intfieldsnames]
    prov.addAttributes(new_fields)
    layer.updateFields()

    for feat in layer.getFeatures():
        for field_name in intfieldsnames:
            feat[f"{field_name}d"] = str(feat[field_name])

        layer.updateFeature(feat)

Is this code faster (tested on a 300 features memory layer) :

layer = iface.activeLayer()

# Get name and type of attributes in layer
intfieldsnames = [i.name() for i in layer.fields() if i.type() in [QVariant.LongLong, QVariant.Int]]

with edit(layer):
    prov = layer.dataProvider()
    new_fields = [QgsField(f"{a}d", QVariant.String) for a in intfieldsnames]
    prov.addAttributes(new_fields)
    layer.updateFields()

    for feat in layer.getFeatures():
        for field_name in intfieldsnames:
            feat[f"{field_name}d"] = f"{feat[field_name]:,}".replace(",", ".")

        layer.updateFeature(feat)
Source Link
J. Monticolo
  • 15.9k
  • 1
  • 29
  • 65

Is this code faster (tested on a 300 features memory layer) :

layer = iface.activeLayer()

# Get name and type of attributes in layer
intfieldsnames = [i.name() for i in layer.fields() if i.type() in [QVariant.LongLong, QVariant.Int]]

with edit(layer):
    prov = layer.dataProvider()
    new_fields = [QgsField(f"{a}d", QVariant.String) for a in intfieldsnames]
    prov.addAttributes(new_fields)
    layer.updateFields()

    for feat in layer.getFeatures():
        for field_name in intfieldsnames:
            feat[f"{field_name}d"] = str(feat[field_name])

        layer.updateFeature(feat)