Using " instead of inches in a field

583
11
01-24-2020 11:05 AM
JimNoble
New Contributor III

Hi guys, I am trying to calculate a field to read 2" HDPE in every row

#Calculate field Conduit feature class Underground
arcpy.CalculateField_management("Underground.dbf", "Conduit",
                                '"2in HDPE"', "PYTHON3")

With this I can get every row to read 2in HDPE but everytime I try to insert " instead of in I get an error. Could you guys help me please!

Thanks!

-Jim Noble

0 Kudos
11 Replies
GeoJosh
Esri Contributor

Jim,

Try this:

arcpy.CalculateField_management("Underground.dbf", "Conduit",
                                '"2\" HDPE"', "PYTHON3")‍‍

You need a forward back slash (\) before the parentheses ("). Otherwise, the Python interpreter will think you are trying to close the string.

Best,

Josh

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Forward slash?  I think "\" is commonly called backslash.

Even with escaping the double quotes, the string still needs to be single quoted and not double quoted.

"'2\" HDPE'"
JimNoble
New Contributor III

Thanks Joshua, your fix worked perfectly.

0 Kudos
JoeBorgione
MVP Esteemed Contributor

Just got back from an ESRI user seminar: at pro 2.5 there finally is Find and Replace functionality!

Personally, I shy away from any special characters in database field values; notice how you need to escape them to get them in there. 'nuff said.....

That should just about do it....
0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

not quite the same... but "unicode" is quite a large world to fully explore.

dq = u"\u201D"

f"quoting{dq}"

'quoting”'

"quoting{}".format(dq)

'quoting”'
0 Kudos
JoeBorgione
MVP Esteemed Contributor
s = '12in pipe'
s = s.replace('in','\"')‍‍

print(s)
12" pipe
That should just about do it....
JimNoble
New Contributor III

Hi Joe, while your fix didn't pop any errors, it didn't actually fix the problem. Maybe I was using it wrong. This is what I put in.

#Calculate field Conduit feature class Underground

arcpy.CalculateField_management("Underground.dbf", "Conduit",
                                '"2in HDPE"', "PYTHON3")

#Set in name replace with quotation

s = '2in HDPE'
s = s.replace('in','\"')
0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Jim, using Joe's code didn't change anything because you ran it after Calculate Field.  The way the code is written now, you are using Calculate Field to update a table and then changing a string within Python but that change is never making it back into a data set.

JimNoble
New Contributor III

I see. Thanks for the explanation!

0 Kudos