Replacing string after a given character but contains a double quote

807
2
02-10-2022 06:35 AM
BenCunningham
New Contributor III

So this is pretty basic for many of you but I'm a novice when it comes to Python and this one's a doozie.

I concatenated two string fields and now I want to remove all text that I added. I separated them with a semicolon to make it clear where the concatenation occurred between the two field values. The problem is I cannot just use !Comments!.replace("; SAP Device Info field: WATER 5/8" ULTRASONIC METER", "") or some similar function because there are double quotes in the string to indicate inches. I'm making it a rule that we never double quotes for inches again, but this was the data I was given.

Not all fields are the same length, so I can't just trim the last x number of characters across the board either.

I read somewhere that I need a back slash near the double quote or to put the double quote inside of single quotes to exempt if from being recognized as part of the expression, but I can't make it work.

Example below:

BenCunningham_0-1644503130849.png

Basically, how can I run through this field and get rid of everything after "; SAP Device Info field:"

How can I tell field calculator to trim after the semicolon? Or is there a better way?

Thanks in advance!

 

 

0 Kudos
2 Replies
JayantaPoddar
MVP Esteemed Contributor

What if you use single quote instead.

!Comments!.replace('; SAP Device Info field: WATER 5/8" ULTRASONIC METER', '')


Think Location
AlfredBaldenweck
MVP Regular Contributor

You can also split at the colon, then take the part before the split.

test1 = "juice:maps"    # input
test2 = test1.split(":")# split at the colon. yields ["juice", [maps"] 
test3 = test2[0]        # grab the first item in the list. "juice"