Hi,
I have a list as follows;
Object ID Field 1
1 Example,Example
2 ,Example,Example,Example
3 ,Example
I want to remove just the first comma.
I would appreciate your assistance.
lots of ways, but a simple code block might do it
<SPAN class="comment token"># ---- code block</SPAN> <SPAN class="keyword token">def</SPAN> <SPAN class="token function">func</SPAN><SPAN class="punctuation token">(</SPAN>a<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">if</SPAN> a<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">==</SPAN> <SPAN class="string token">','</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">return</SPAN> a<SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">:</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">return</SPAN> a <SPAN class="comment token"># --- 'a' is your field name with the python parser, the expression would be</SPAN> <SPAN class="comment token"># func(!YourFieldNameHere!</SPAN> a <SPAN class="operator token">=</SPAN> <SPAN class="string token">'Example,Example'</SPAN> a <SPAN class="string token">'Example,Example'</SPAN> b <SPAN class="operator token">=</SPAN> <SPAN class="string token">',Example,Example,Example'</SPAN> func<SPAN class="punctuation token">(</SPAN>b<SPAN class="punctuation token">)</SPAN> <SPAN class="string token">'Example,Example,Example'</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
Or more simply as a straight field calculation... python parser
replace 'a' with your field name in exclamation marks as above
a <SPAN class="operator token">=</SPAN> <SPAN class="string token">',Example,Example,Example'</SPAN> <SPAN class="punctuation token">[</SPAN>a<SPAN class="punctuation token">,</SPAN> a<SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">:</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">[</SPAN>a<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">==</SPAN> <SPAN class="string token">','</SPAN><SPAN class="punctuation token">]</SPAN> Out<SPAN class="punctuation token">[</SPAN><SPAN class="number token">27</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">'Example,Example,Example'</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
Hi Dan, thanks for this however it doesn't seem to work. I replaced 'a' with my field name. I am using field calculator. Do i have to add anything in codeblock?
Set the field calculator to following for the second example
And if you are getting an error, then show it.
As for the code block, nothing needs to be changed, but your expression box would call the function with
func(!fieldnamehere!)
Of course, python parser and you are calculating values in a new text field.
Done it. Thanks.
In case you have multiple commas at the start you could use this function in the code block:
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">EliminateStartignCommas</SPAN><SPAN class="punctuation token">(</SPAN>txt<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">while</SPAN> txt<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">==</SPAN> <SPAN class="string token">','</SPAN><SPAN class="punctuation token">:</SPAN> txt <SPAN class="operator token">=</SPAN> txt<SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">:</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="keyword token">return</SPAN> txt<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
You could also use txt.lstrip(",")
And just to play devils advocate.... What about a find and replace for " , " at the start of the field?
To be completely inclusive
txt <SPAN class="operator token">=</SPAN> <SPAN class="string token">"!A simple question becomes a Monty Python Circus"</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="string token">'Good'</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">'Grief!!!'</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">[</SPAN>txt<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="keyword token">in</SPAN> '!"<SPAN class="comment token">#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~']</SPAN> <SPAN class="string token">'Grief!!'</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
Hi All,
I have a somewhat similar question that I have problem solving
Object ID Field
1 Apple.
2 Orange.
3 Egg
I would like to remove the trailing "." if any. However my code below simply check the string and remove all last characters.
Trim(Left([Field], LEN([Field])-1))
Appreciate any help.
Cheers
Easiest way is probably using a Python expression, something like !field!.rstrip(".")
IF right([Field],1) = "." then
[Field] = Trim(Left([Field],LEN([Field])-1))
ELSE
[Field]=Trim([Field])
END IF
Les membres connectés peuvent publier, suivre les mises à jour, et plus encore. Nouveau ici ? Inscrivez-vous gratuitement.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.