Select to view content in your preferred language

I NEED TO EXTRACT THE FIRST LETTER AND THE MIDDLE LETTER OF A STRING ATTRIBUTE VALUE

4013
20
Jump to solution
05-23-2013 01:57 PM
OLANIYANOLAKUNLE
Occasional Contributor II
I NEED TO EXTRACT THE FIRST LETTER AND THE MIDDLE LETTER OF A STRING ATTRIBUTE VALUE I.E "FIGURINE" , I WANT TO EXTRACT LETTER "F" & "R" TO FORM "FR". PLEASE HELP ME
Tags (2)
0 Kudos
20 Replies
T__WayneWhitley
Frequent Contributor
So have you already got this in a format you can read in ArcMap?...if so, have you tested that query to select the records you want to delete?  I'm not sure what you are saying, are you stuck somewhere in this process?

If that query works, i.e., you've opened the attribute table and switched selection (or used 'not like' in your query) and satisfactorily confirmed this is what you want to keep, then execute the delete process.  On the remaining set, then execute the calculate field process, as in the following (I didn't test it, but will follow Chris example, which looks good to me):

arcpy.CalculateField_management(myFC,"NEW_FIELD","!OLD_FIELD!.split(';')[1][0:-1]","PYTHON_9.3","")


....make the necessary adjustments in field calculator syntax; essentially this is the operative Python syntax to 'extract' your text:
.split(';')[1][0:-1]
0 Kudos
OLANIYANOLAKUNLE
Occasional Contributor II
Please how do I compose this arcpy script to select any text that has "ha", I tried the script below and that is the error message I got. Thanks

>>> arcpy.SelectLayerByAttribute_management("HTNT_Annoss", "NEW_SELECTION",'"TEST" LIKE '%ha%'')
Runtime error
Traceback (most recent call last):
  File "<string>", line 1, in <module>
NameError: name 'ha' is not defined
>>>
0 Kudos
T__WayneWhitley
Frequent Contributor
...right, Python thinks you want variable substitution...number of different ways to handle this, but try:

'"TEST" LIKE \'%ha%\''
0 Kudos
OLANIYANOLAKUNLE
Occasional Contributor II
Thanks Y'all I got the SQL Expression eventually;

arcpy.SelectLayerByAttribute_management("Annos", "ADD_TO_SELECTION",'"TEXTS" LIKE \'%m%\'')

Thanks a bunch!!!!

Please how can i handle the issue of schema Lock? This schema lock issue is really getting on my nerves.
0 Kudos
T__WayneWhitley
Frequent Contributor
Well, I guess the shortest answer to combat schema locks is not to have the dataset (the feature class) open in another application.

Things like adding a field I think will require what is called an 'exclusive' lock; other operations may allow what is known as a 'shared' lock.  However, it is safe not to allow anything to remain open or any other users accessing your data if you're trying to edit it.  (or, if you must allow other users, you may work with versioning, distributed editing, etc.)

Also, if you have done something like open a cursor and you're done with it, that is an object reference you can delete - not sure if that always works in a timely fashion, but it helps to remember if you've opened any object for read/write purposes that it can 'lock' it.


Hope that helps.  Is something or some part of your procedure causing the locking of your dataset?

Enjoy,
Wayne
0 Kudos
OLANIYANOLAKUNLE
Occasional Contributor II
Please which symbol can I use to represent the degree (°) and minute (') symbol in my python scripts, because each time I try to run my script I get this error message for the line containing the 2 symbols mentioned earlier;
\line 32, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details

Any suggestions?
0 Kudos
OLANIYANOLAKUNLE
Occasional Contributor II
How can i split the text for the ones like this \pt16.25;{\fArial|b0|i0|c0|p34;15.00m} (they have 2 😉 and the script ive been using is like this
arcpy.CalculateField_management("Annos","TEXTS","!TEXTSTRING!.split(';')[1][0:-1]","PYTHON_9.3","")

Please help me
0 Kudos
JasonScheirer
Regular Contributor II
You can use regular expressions to take out the control codes in one fell swoop.

import re

def remove_codes(code_string):
    # Get rid of enclosed text, ie {\instruction;Text to keep}
    replaced_sequences = re.sub(r"{\\[^;]+;([^}]*)}", r"\1", code_string)
    # Get rid of standalone \font|22pt; like sequences
    new_string = re.sub(r"\\[^;]+;", "", replaced_sequences)
    return new_string


And this gives us

>>> remove_codes(r"\pt16.25;{\fArial|b0|i0|c0|p34;15.00m}")
'15.00m'


in the Python window
0 Kudos
OLANIYANOLAKUNLE
Occasional Contributor II
Thanks jscheirer I tried to insert your code into my script and this was the error message I got;

arcpy.CalculateField_management("Annos","TEXTS","!TEXTSTRING!.remove_codes","PYTHON_9.3","")
Runtime error  Traceback (most recent call last):   File "<string>", line 1, in <module>   File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\management.py", line 3128, in CalculateField     raise e ExecuteError: ERROR 000539: Error running expression: "{\fArial|b0|i0|c0|p34;C - 40}".remove_codes  Traceback (most recent call last):   File "<expression>", line 1, in <module> AttributeError: 'str' object has no attribute 'remove_codes'  Failed to execute (CalculateField).


Please what do you think I can do?
0 Kudos
JasonScheirer
Regular Contributor II
You put that part with the import and def in the code block and change the expression to remove_codes(!TEXTSTRING!)
0 Kudos