0

I run this simple script, line-by-line, and I get the error

Runtime error SyntaxError: can't assign to literal (, line 1)

What am I doing wrong?

import arcpy
# Set workspace
arcpy.env.workspace = "D:/pro-restaura/ProRestaura-dados/outout-mg/reproject-mg"

# Set local variables
in_features = (arcpy.Raster, "area_pro_restaura_minas.tif")
out_feature_class = "D:/pro-restaura/ProRestaura-dados/outout-mg/reproject-mg/class_UpVeg.tif"
where_clause = "Value" = 2103

# Execute Select
arcpy.Select_analysis(in_features, out_feature_class, where_clause)
3
  • A syntax error is a syntax error. You've attempted to assign the integer value 2103 to the string constant "Value". String literals with quotes in them need to be quoted: where_clause = '"Value" = 2103'
    – Vince
    Commented Jan 21, 2019 at 13:33
  • @Vince although there is a syntax error, it will not work after fixing the expression because the OP uses a tool designed for vector data not for raster data.
    – ahmadhanb
    Commented Jan 21, 2019 at 13:38
  • Yes, and there's a third error, not mentioned in the question and only reflected in comments, trying to use the equivalence operator with a list of terms.
    – Vince
    Commented Jan 21, 2019 at 15:34

3 Answers 3

3

You need to use Extract by Attributes function if you have Spatial Analyst to extract specific values from raster data:

Here is a code snippet from the help above:

import arcpy
from arcpy import env

# Import Spatial Analyst extension
from arcpy.sa import *

# Specify the workspace
env.workspace = r"F:\Ahmad\Test\ASTGTM2_N22E093"

# Use the extract function. "test_dem" is the name of the raster file
# with esri grid format
# If you want to extract multiple values at once, you have to write
# it like this: "VALUE in (500,1000,1500)"
attExtract = ExtractByAttributes("test_dem", "VALUE in(500,1000,1500)")

# Save the output raster file. No extension means esro grid as input file
attExtract.save(r"F:\Ahmad\Test\ASTGTM2_N22E093\test_dem_2")

You need to adapt your code based on the code above.

This the attribute table of the input raster "test_dem":

enter image description here

The attribute table of the output raster "test_dem_2":

enter image description here

Here is how it looks in the table of content:

enter image description here

4
  • Sorry, but I don't understand. I don't want to do spatial analys, I just want to select in the attribute table by value. "where_clause = ' "Value" = ('2103', '2109', '1405', 2103') ' Commented Jan 21, 2019 at 13:24
  • arcpy.Select_analysis deals only with vector data. If you need to extract values from raster data, you need to use arcpy.sa.ExtractByAttributes(). To use the function, you need to have Spatial Analyst extension.
    – ahmadhanb
    Commented Jan 21, 2019 at 13:29
  • Thank you very much to try to help me! I made changes to the methods and the declaration form of the string (Value) and integer values (2103, 2109), but doesnt seems to work Here it is: import arcpy from arcpy import env   env.workspace = "D: \ pro-restore \ pro-data-outout-mg \ reproject-mg" attExtract = ExtractByAttributes ('MG_2015_2017.tif', '"Value" = (2103,2109,1405,2103)' attExtract.save ("D: \ pro-restore \ ProRestore-data \ outout-mg \ reproject-mg \ class_UpVeg.tif") Commented Jan 21, 2019 at 13:41
  • I updated my answer. If you don't have Spatial Analyst, it will not work. You need to add this line from arcpy.sa import * if you have Spatial Analyst.
    – ahmadhanb
    Commented Jan 21, 2019 at 14:01
1

You have a problem with the where_clause variable. You should surround the clause with quotations.

where_clause = '"Value" = 2103'
2
  • Now I understand, mr.@Mohannad ! I'm really appreciate your help! Unfortunately, I'm still stuck with a simple problem setting up my workspace. I use `env.workspace = r 'D: / pro-restore / ProReset-data / ProReset' And I get the message "Parsing error SyntaxError: invalid syntax (line 1)". I tried to invert the bars in writing the path, but still the same error happens. Commented Jan 21, 2019 at 14:43
  • 1
    @Lilian I don't think that the problem is in this line. To be sure try to run it seperately preceded by "import arcpy" and "from arcpy import env". Commented Jan 21, 2019 at 16:10
0

you can use this clause, for example, attExtract = ExtractByAttributes("test_dem", "VALUE=500 OR value=1000 OR value=1500")

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.