I have a problem with attribute table which I'd like to learn to solve without MS Excel.

The Shapefile I have, has an attribute field, which consists of 3 parts, separated by ";" -sign. Here is an example of what the original looks like, how it is split and how the final attribute should look like. Red is the original and Blue is what it looks like at the end.

enter image description here enter image description here

So what I would like to do, is to edit "RATAOSOITE" with a single python or VBScript in Arcmap. The script should do the following:

a) remove the first number/text before the first ";" sign. b) change all empty spaces to "Underscore" and change the second ";" -sign to "Underscore".

I have next to no experience with scripting, so this maybe rather basic task is rather hard for me. I have done the same thing with Excel. Copy Attribute table to Excel --> text to columns by ";" --> replace "space" with "_(Underscore)" and "CONCATENATE(a1;Underscore;b1)" or something like that. But it would be great, if I could do this all in Arcmap Field Calculator.

  • Hi, like I mentioned in the question, i have next to no experience with scripting. That's why I'm asking, since I have done the whole thing with copy-paste to excel and from excel, but I suspect there is a better alternative for this task by making a script. Just don't have any clue where to start. – Sisuaski Oct 16 '17 at 10:10
  • 2
    You need to show an attempt or your question will be closed. You can take a look at the UpdateCursor (pro.arcgis.com/en/pro-app/arcpy/data-access/…) and Python String split() Method (tutorialspoint.com/python/string_split.htm) – BERA Oct 16 '17 at 10:14

This is a relatively trivial find and replace problem.

  1. In your shapefile add the new text field (what you are calling RAIDETUNN).
  2. Run the field calculate tool on your new field, add into the code block the following code as shown below (obviously your input table will have a different name!):

Field Calculate

The code is here:

def cleanup(s):
    myList = s.split(";",3)
    text= myList[1] + "_" + myList[2]
    text = text.replace(" ","_")
    return text
| improve this answer | |
  • Hi, Many thanks for your answer! I tried to apply it through Calculate Field tool, but something went wrong. It says: ERROR 000539, SyntaxError, line1. I was also wondering, "myList = s.split (";",3) --> that number 3 in the sentence. Those numbers or letters are not always 3 characters long, they vary between 3 to 7 or more before the first ";" sign. – Sisuaski Oct 16 '17 at 12:42
  • The 3 refers to splitting the input text into 3 items of a list so A;B;C will split into 3 separate sections of text separated by a semicolon. It has nothing to do with the length of the text separated by semicolons. The error is suggesting that your input text is not as you described, i.e. 3 sections of text separated by 2 semicolons or you made a typo. – Hornbydd Oct 16 '17 at 13:20
  • Hi, thanks for your patience. I added a new photo to your post, to show what I'm trying to do and the error I get. I even tried to create a new layer with ArcMap, added few objects and edited their attributes to be similar to "text";"text";"text -theme. Even with the new layer I get an error while trying to process the information. Any ideas what am I doing wrong? – Sisuaski Oct 17 '17 at 7:16
  • Ha! In the expression parameter you must call the function which I called cleanup simply as "cleanup(!RATAOSOITE!)" and not as "def cleanup(!RATAOSOITE!)" as you have. I tried to construct an image to help you but never actually ran the code as I do not have the same data, so in my image I had introduced a bug, my apologies. I will correct that (also removed your image as it is not really part of the answer, but it was essential for me to see it.) – Hornbydd Oct 17 '17 at 8:48
  • 1
    Thank you! Now it works like a charm! Had to change some attributes, which were missing the second ";" sign. I suppose it's easier to change those few attributes than to write a script that recognizes bad attributes like "001;001 KYT;0001" --> Pass, split to 3 parts... "001;001 KYT"-->Fail, split to 2 parts.. But this python code saves me a huge amount of work, many thanks! – Sisuaski Oct 18 '17 at 7:19

Not the answer you're looking for? Browse other questions tagged or ask your own question.