3

I'm new to Python scripting and I don't understand what is wrong with this code. I want to concatenate the string values from GRID1MIL and GRID100K. I've created a text column MGRS to begin with.

enter image description here

enter image description here

I also tried writing the following but it did not work:

enter image description here

enter image description here

Using VB Script I was able to do it easily but I want to learn how it's done with Python.

enter image description here

3
  • What error do you see when you run one or other of your attempts? Let's focus on that because there is little point showing two methods and reporting no symptoms.
    – PolyGeo
    Aug 23, 2017 at 2:54
  • 3
    Simple concatenation is !grid1mil! + !grid100k!. The .join method is for list variables, for example ','.join(['a','b','c']) becomes 'a,b,c'. Or you could mean os.path.join() which joins paths with your OS sepchar (usually '\') to make up a path from two or more parts. Your AddIt code is close but your indentation isn't consistent, return c should be in line with c = a + b. Aug 23, 2017 at 2:55
  • @MichaelStimson: That was it. I can mark your answer as correct if you create one (copy paste).
    – val
    Aug 23, 2017 at 3:10

3 Answers 3

3

I think you're almost there, but you need to get your indentation consistent. Indentation is very important in Python and defines the scoping/grouping of statements. Suggested indentation is to add four spaces for every indentation level.

try keeping the same indentation for lines: c=a+b and return c

like this:

def addIt(a,b)
    c = a + ' ' + b
    return c
1
  • 2
    4 spaces might be a good place to start but you'll very quickly run off the right side of the page as you become more proficient and use with blocks and try/except levels... I use 1 Tab which I can configure in my editor to be 1 to 4 space width (or more); start with 4 and as your code starts creeping right reduce to 2 space widths without having to modify your code at all. Aug 23, 2017 at 3:25
2

You can use + to concatenate strings as per the accepted answer.

To expand a bit on what caused your syntax error - join is a method of a string object and is used to concatenate elements of an iterable (list, tuuple, etc) with that string.

i.e.

' '.join(['x', 'y', 'z'])
x y z
', '.join(['x', 'y', 'z'])
x, y, z
d = ' | '
d.join(['x', 'y', 'z'])
x | y | z

Using + is perfectly fine, but you might want to consider .join when you have lots of things to join or your list of things to join might not always be the same length.

So to get your original expressions working in the field calculator, you could use:

''.join([!GRID1MIL!, !GRID100K!]) 

Or (in the code block):

def addIt(a,b)
    return ''.join([a,b])

addIt(!GRID1MIL!, !GRID100K!) 
0

This ESRI blog post has code that describes how to concatenate fields when you aren't sure how many fields will need to be concatenated. For example sometimes you have two values, sometimes you have three.

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.