5

I need to copy the attribute value (only one particular attribute) of a selected point and save it in a text file. Is there any Python script or plugin which can do this for me? I need to save a lot of points so individually copying the values is going to be tedious.

1 Answer 1

4

If you only want to save values from one particular field, you could use something like the following in the Python Console:

def save(fieldName):
    result_path = 'path/to/file.txt'
    layer = iface.activeLayer()
    result = ''
    for feat in layer.selectedFeatures():
        result += str(feat[fieldName]) + '\n'
    with open(result_path, 'a') as f:
        f.write(result)

Then select your features and type save('myField') to run the function (note the single quotes around the field name).


Personally, I would also include the id of the feature so that it can be matched to the attribute. So if you wanted, you could replace:

result += str(feat[fieldName]) + '\n'

with

result += str(feat.id()) + ': ' + str(feat[fieldName]) + '\n'
6
  • Thanks, but this keeps giving me errors. It says error in syntax at lines 1, 3 and 5 and 6 randomly. Found out what was wrong.
    – Sunny Naik
    May 30, 2017 at 10:20
  • @SunnyNaik - Did you set the path of the text file and the field name correctly?
    – Joseph
    May 30, 2017 at 10:25
  • Yes, the file is getting created. But there is nothing stored in it.
    – Sunny Naik
    May 30, 2017 at 10:28
  • @SunnyNaik - I cannot replicate your issue, it's working fine for me. Are you sure selected your features before running the code?
    – Joseph
    May 30, 2017 at 10:43
  • Yes I selected the features, only one time it got saved, the rest of the times it did not.
    – Sunny Naik
    May 30, 2017 at 10:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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