Select to view content in your preferred language

Convert expression in Calculate Field from VB to PYTHON3

1663
9
Jump to solution
11-24-2021 10:18 AM
SoratoSouza_e_Silva
Frequent Contributor

Hi, 

I'm having trouble converting this tool's expression to Python 3, could someone help me?

arcpy.CalculateField_management("selecionado_View", "selecionado_" + Safra + ".SELECIONADO", "[selecionado_" + Safra + ".SELECAO] + [selecionado_" + Safra2 + ".SELECAO]", "VB", "")

I'm not sure what this dot means.

Thank´s

Tags (3)
0 Kudos
1 Solution

Accepted Solutions
curtvprice
MVP Esteemed Contributor

@SoratoSouza_e_Silva wrote:

Hi, 

I'm having trouble converting this tool's expression to Python 3, could someone help me?

arcpy.CalculateField_management("selecionado_View", "selecionado_" + Safra + ".SELECIONADO", "[selecionado_" + Safra + ".SELECAO] + [selecionado_" + Safra2 + ".SELECAO]", "VB", "")

I'm not sure what this dot means


What the dot means, from the help for Add Join:
In the resulting input table, fields will be prefixed with the input's name and a period (.), and all fields from the join table will be prefixed with the join table name and a period as the default.

Here's a crack at a Python 3 conversion. I second the suggestion in this thread that playing around with the Calculate Field tool interactively would help you learn some of the ins and outs of this, especially if you try Copy as Python command after filling out the tool dialog. A key thing to keep in mind is the python string should be directly interpretable as a Python expression (with the exception of field names surrounded by ! characters) and data types are not converted for you as in VB.

 

# I am assuming Safra, Safra2 are Python variables of type string.
tbl = "selecionado_" + Safra  # ex. selecionado_foo
tbl2 = "selecionado_" + Safra2 # ex. selecionado_foo2
# expr below becomes:
#  "!selecionado_foo.SELECAO! + !selecionado_foo2.SELECAO!"
expr = "!{}.SELECAO! + !{}.SELECAO!".format(tbl, tbl2)
arcpy.CalculateField_management("selecionado_View", 
    tbl + ".SELECIONADO", expr, "PYTHON") 

 

 

View solution in original post

9 Replies
MichaelVolz
Esteemed Contributor

Do you have the ability to generate this query in Pro SelectbyAttribute interface using python selection and then review the SQL query itself with the toggle?

0 Kudos
SoratoSouza_e_Silva
Frequent Contributor

more  less I can´t simulate this field: 

selecionado_" + Safra + ".SELECIONADO", "[selecionado_" + Safra + ".SELECAO]

0 Kudos
MichaelVolz
Esteemed Contributor

This looks like a field concatenation process.  Can you list the field names and the data type next to it?

0 Kudos
SoratoSouza_e_Silva
Frequent Contributor

Safra = Text

Safra2 = Text

SELECAO = Text

"selecionado_" just string to concatenate

 

Thank´s

0 Kudos
MichaelVolz
Esteemed Contributor

Performing this exercise in Pro itself, as opposed to just adding it to a script, would be helpful to you to learn the syntax that is required per Dan's replies.

DanPatterson
MVP Esteemed Contributor

From the links I sent earlier, in Python field names are enclosed in exclamation marks

eg   Safra  !Safra!


... sort of retired...
0 Kudos
SoratoSouza_e_Silva
Frequent Contributor

Yes correct but I have problem after of the concatenate: selecionado_" + Safra, Safra = 2122 (Text)

NameError: name 'selecionado_2122' is not defined

0 Kudos
curtvprice
MVP Esteemed Contributor

@SoratoSouza_e_Silva wrote:

Hi, 

I'm having trouble converting this tool's expression to Python 3, could someone help me?

arcpy.CalculateField_management("selecionado_View", "selecionado_" + Safra + ".SELECIONADO", "[selecionado_" + Safra + ".SELECAO] + [selecionado_" + Safra2 + ".SELECAO]", "VB", "")

I'm not sure what this dot means


What the dot means, from the help for Add Join:
In the resulting input table, fields will be prefixed with the input's name and a period (.), and all fields from the join table will be prefixed with the join table name and a period as the default.

Here's a crack at a Python 3 conversion. I second the suggestion in this thread that playing around with the Calculate Field tool interactively would help you learn some of the ins and outs of this, especially if you try Copy as Python command after filling out the tool dialog. A key thing to keep in mind is the python string should be directly interpretable as a Python expression (with the exception of field names surrounded by ! characters) and data types are not converted for you as in VB.

 

# I am assuming Safra, Safra2 are Python variables of type string.
tbl = "selecionado_" + Safra  # ex. selecionado_foo
tbl2 = "selecionado_" + Safra2 # ex. selecionado_foo2
# expr below becomes:
#  "!selecionado_foo.SELECAO! + !selecionado_foo2.SELECAO!"
expr = "!{}.SELECAO! + !{}.SELECAO!".format(tbl, tbl2)
arcpy.CalculateField_management("selecionado_View", 
    tbl + ".SELECIONADO", expr, "PYTHON") 

 

 

SoratoSouza_e_Silva
Frequent Contributor

Very good! Thank´s @curtvprice help a lot!

0 Kudos