3

I am creating a model which will give me two string values, one which reads exactly what is in a field and another which automatically replaces any Space with an "_". I have used Calculate Value tool, and entered in the following:

Expression:

ReplaceName(%Value%)

Code Block:

def ReplaceName(name):
 return name.replace(" ", "_")

The result is an error:

ERROR 000539: Error running expression: ReplaceName(NoSpace1) 
Traceback (most recent call last):
 File "<expression>", line 1, in <module>
NameError: name 'NoSpace1' is not defined

I suspect because not all my Values have spaces

Model and table

How can I make this work?

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Apr 18, 2016 at 16:54

3 Answers 3

3

You don't necessarily have to define a function for this one. Try this:

str(!PlaceName!).replace(' ', '')

The "not defined" error is related to the lack of quotes around the NoSpace1. This could be from the % on either side of the Value. Your expression should be

ReplaceName(!Placename!)
answered Apr 18, 2016 at 17:35
3
  • or str(!PlaceName!).replace(' ', '_') for underscores Commented Apr 18, 2016 at 18:17
  • Thanks for your answer! however it didn't work! I tried several variations including the example you gave. Executing (Calculate Value): CalculateValue str(!PlaceName!).replace(" ", "") # String ERROR 000539: SyntaxError: invalid syntax (<expression>, line 1) Executing (Calculate Value): CalculateValue str(!Value!).replace(" ", "") # String ERROR 000539: SyntaxError: invalid syntax (<expression>, line 1) Commented Apr 18, 2016 at 18:40
  • Try double clicking the field name to put it in the expression; sometimes a dataset will have a field name prefix that doesn't show up in the attribute table. Also try changing the double quotes to single quotes. Also for the parser make sure you're using the PYTHON_9.3 option. Is the field calculator returning any values at all? Commented Apr 18, 2016 at 20:41
1

You do not need the field calculator for replace at all.

You can just use the Find & Replace tool which is accessed from the attribute-table which has the search/replace function the same way as in Excel, for example.

It also works for all columns simultaneously or can be set to search for whole values etc.

enter image description here

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
answered Apr 18, 2016 at 20:41
1
  • Thanks for your answer, but I need this to work inside of model builder, also I am not trying to change the value in the table. Ultimately both the value with the space and the value without a space are going to be used as inputs in a later portion of the model. Commented Apr 19, 2016 at 13:12
0

This may not be the most elegant of fixes, but it did work:

enter image description here

I found that the %Value% had to have Quotes and I used a try/except sequence to get around the possible lack of a space in some of the values.

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
answered Apr 19, 2016 at 14:20

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.