I have some addressing data that I would like to update and one of the tasks is to concatenate a new full street address field [ADDR1] by combining several of the component fields. I know how to do this in VBScript, but figured this would be a good example to work out in Python, as I don't use Python often and have alot to learn. My programming background is FORTRAN and BASIC, and in the years since those were popular hack and slash adapting code for AML/Avenue/VBA/VB.Net - so assume I know little about Python.
What is desired (psuedo-logic):
ADDR1 = STRNUM + STRNUMSUF + STRPREDIR + STRNAME + STRTYPE + STRSUFDIR
I'm using ArcGIS 10.2.1 and the data shown here is test data in a File Geodatabase feature class.
After poking around various Help articles, it appears that in Python the .format method in would work. However, when I run it in Field Calculator the result looks good except "None" gets concatenated as text if the original field value was NULL (this issue doesn't come up with VBScript). I'd like any values of NULL to instead just be blank - i.e. ignore them entirely. For example, for the first record the desired outcome in ADDR1 is "224 D N CHURCH ST" not "224 D N CHURCH ST None".
One of the Python articles I found mentions the NULL issue:
Source: Concatenating field values using Calculate Field | ArcGIS Blog
Is there a different way to do the concatenation in Python to achieve the desired outcome? I'm trying to wrap my head around how Python approaches this, particularly in regards to using it in ArcMap in Field Calculator.
Lastly, can you recommend a good guide for beginners that explains how to use Python for these sort of issues?
Thanks,
Chris Donohue, GISP
Solved! Go to Solution.
I figured out how to make tables with nulls...not useful for me, but if you have them, here is an example
Field calculator expression:
" ".join([str(i) for i in [ !prefix_txt!, !number_int!, !main_txt!, !numb_dble!] if i])
The operative part is if i ])
I don't have nulls in my fields, but you can glean some ideas from the options available below
>>> a = 12345 >>> b = None >>> c = "some text" >>> d = "" >>> e = "more" >>> frmt = "{} {} {} {}".format(a,b,c,d,e) >>> frmt2 = "{!s:} {!s:} {!s:} {!s:}".format(a,b,c,d,e) >>> frmt3 = "{!r:} {!r:} {!r:} {!r:}".format(a,b,c,d,e) >>> fld = " ".join([str(i) for i in [a,b,c,d,e] if i]) >>> >>> frmt '12345 None some text ' >>> frmt2 '12345 None some text ' >>> frmt3 "12345 None 'some text' ''" >>> fld '12345 some text more' >>>
note the last option is the only one that deals with None explicitly, so if your null values are equivalent to Python None, then this should work.
PS you might have to do the string trimming thing is to get rid of preceeding or following spaces
I figured out how to make tables with nulls...not useful for me, but if you have them, here is an example
Field calculator expression:
" ".join([str(i) for i in [ !prefix_txt!, !number_int!, !main_txt!, !numb_dble!] if i])
The operative part is if i ])
The operative part is if i
In short:
if i: # checks if i 'exists' do something
It can also be interpreted as if i IS not just exists. I posted a blog on nothingness a while ago
I can't find your nothing post in your prolific blog. How does IS differ from EXISTS?
None isn't...nor is 0 or 1 ... more explorations into geometry
Before I forget ... # 5 ... That empty feeling...
There are links within those and some comments and a blog post by Joshua Bixby which you may find interesting to.
Also the concepts of "exists" and "is" come out of philosophy and can be exemplified in studies of symbolic logic. Truth values complicate yet supplement these... You might be familiar with phrase "I think therefore I am" and the debate that still continues to this day. Sooo philosophy meets geomatics perhaps
I forget which comedian came up with this adaption of a famous quote on existence:
"I think, therefore I am...... I think?" (unsure)
Chris Donohue, GISP
Ah, so that is why the result was different. If i sees Null as non-existing, so no join (and therefore no substitution text of 'None'). Makes sense.
Chris Donohue, GISP
Dan's one-line example actually has a whole bunch of things going on.
fld = " ".join([str(i) for i in [a,b,c,d,e] if i])
... can be rewritten:
myList = [] for i in [a,b,c,d,e]: if i: myList.append(str(i)) " ".join(myList)
Not sure if that helps or not, just a different way to read it.