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

3900
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
1 Solution

Accepted Solutions
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]

View solution in original post

0 Kudos
20 Replies
RhettZufelt
MVP Frequent Contributor
strings are iterable, so the first index will be the first letter.
then I get the total number of characters, divide by two, and extract that one.



>>> string = "FIGURINE"
>>> string[0]
'F'
>>> string[len(string)/2]
'R'
>>> string[0] + string[len(string)/2]
'FR'
>>>

R_
0 Kudos
ChrisSnyder
Regular Contributor III
"Field calculator expresion might look like:".upper()

arcpy.CalculateField_management(myFC,"NEW_FIELD","!OLD_FIELD![0] + !OLD_FIELD![len(!OLD_FIELD!)/2]","PYTHON_9.3","")
0 Kudos
by Anonymous User
Not applicable
"Field calculator expresion might look like:".upper()


Haha- Too funny! There needs to be a "Like" button for situations like this...
0 Kudos
OLANIYANOLAKUNLE
Occasional Contributor II
Thanks a bunch, csny490 & rzufelt   your suggestions worked perfectly for me.
0 Kudos
OLANIYANOLAKUNLE
Occasional Contributor II
I have alot of annotation text attributes imported from AutoCAD, i want to delete all the annotations like the following;

{\fArial|b0|i0|c0|p34;14.50m}
{\fArial|b0|i0|c0|p34;\L0.0203 ha}
{\fArial|b0|i0|c0|p34;161°                                           34'}
{\fArial|b0|i0|c0|p34;93°                                                10'}

and i want to delete the values such as this {\fArial|b1|i0|c0|p34;} and leave 426 for the ones that appear like this {\fArial|b1|i0|c0|p34;426} and the same for {\fArial|b0|i0|c0|p34;KAF 8816}, i want to delete {\fArial|b0|i0|c0|p34;} and leave KAF 8816.

Please do you have any suggestions. Thanks
0 Kudos
T__WayneWhitley
Frequent Contributor
You mean something like this?:
>>> text = ['{\fArial|b0|i0|c0|p34;14.50m}',
 '{\fArial|b0|i0|c0|p34;\L0.0203 ha}',
 '{\fArial|b0|i0|c0|p34;161° 34}',
 '{\fArial|b0|i0|c0|p34;93° 10}']
>>> for anno in text:
 anno.split(';')[1]

 
'14.50m}'
'\\L0.0203 ha}'
'161\xb0 34}'
'93\xb0 10}'
>>> 


...ah, but think you want to get rid of the last bracket character too -- use the combo 'split' and string slicing py methods:
>>> for anno in text:
 anno.split(';')[1][0:-1]

 
'14.50m'
'\\L0.0203 ha'
'161\xb0 34'
'93\xb0 10'
>>> 
0 Kudos
OLANIYANOLAKUNLE
Occasional Contributor II
I want to delete all the first set of annotations i.e

{\fArial|b0|i0|c0|p34;14.50m}
{\fArial|b0|i0|c0|p34;\L0.0203 ha}
{\fArial|b0|i0|c0|p34;161° 34'}
{\fArial|b0|i0|c0|p34;93° 10'}

and for these ones, I want to remove the unwanted characters and keep the ones I need i.e delete the values such as this {\fArial|b1|i0|c0|p34;} and leave 426 & the same for {\fArial|b0|i0|c0|p34;KAF 8816}, i want to delete {\fArial|b0|i0|c0|p34;} and leave KAF 8816, something of this nature.
0 Kudos
T__WayneWhitley
Frequent Contributor
Well, there must be a way to select the annos to be deleted?
As for the portion you want to keep, following Rhett's example above, you could apply the split/slice technique I already demonstrated.  I was just using your sample text in a list....you can apply it also as Chris suggested in a Python field calculation - in this bare-bones example, say you want to keep '14.50m':
>>> text = '{\x0cArial|b0|i0|c0|p34;14.50m}'
>>> text.split(';')[1][0:-1]
'14.50m'
>>>


That's not the only way to do it, but fair enough... looks like your bigger challenge will be the means to eliminate the anno you want to keep.

(er, I mean, eliminate the anno you don't want to keep.)
0 Kudos
OLANIYANOLAKUNLE
Occasional Contributor II
I want to use an sql expression like this: "TEXTSTRING" LIKE '%fArial|b0|i0|c0|p34%' to remove all the annotations like that, then delete the values such as this {\fArial|b1|i0|c0|p34;} and leave 426 for the ones that appear like this {\fArial|b1|i0|c0|p34;426} and the same for {\fArial|b0|i0|c0|p34;KAF 8816}, i want to delete {\fArial|b0|i0|c0|p34;} and leave KAF 8816.
0 Kudos