Select to view content in your preferred language

Why does my calculate field script remove the underscore

361
1
Jump to solution
03-04-2024 08:57 AM
BrandonMcAlister
Frequent Contributor

I am going to try and keep this simple.

I am pulling information from table A which has a text field with numbers like this 1119_232.2_93.2.

I get this information by using arcpy.da.Searchcursor._as_narray(). I then access the item from the array and save it as a string. 

Then my script runs the calculate field tool which calculates a specific field based on the string. 

I specifically send the numbers in as a string by using str()

However without fail every time it removes the "_" and write the numbers to the TEXT field as 1110232.293.2

parcels = "Parcels (Block and Lot) Data - For Internal Purposes Only - Do Not Distribute"
i = 0
cursor = arcpy.da.SearchCursor(Geocode, ["ObjectID"])
array = cursor._as_narray()
while i < len(array):
  arcpy.management.SelectLayerByAttribute(Geocode, where_clause = "ObjectID = " + str(array[i][0]))
  arcpy.management.SelectLayerByLocation(
    in_layer="'Parcels (Block and Lot) Data - For Internal Purposes Only - Do Not Distribute'",
    overlap_type="INTERSECT",
    select_features="CSRR_PI_Coords_Geocoded",
    search_distance=None,
    selection_type="NEW_SELECTION",
    invert_spatial_relationship="NOT_INVERT"
  )
  pcursor = arcpy.da.SearchCursor(parcels, ["Pams_Pin"])
  parray = pcursor._as_narray()
  string = str(parray[0][0])
  splstr = string.split("_")
  t = 1
  concat = splstr[0]
  if len(splstr) > 3:
  while t < len(splstr) - 1:
    concat += "_" + splstr[t]
    t += 1
  else:
    while t < len(splstr):
    concat += "_" + splstr[t]
    t += 1
  print(concat)

  arcpy.management.CalculateField(
    in_table=Geocode,
    field = "USER_parcel_data",
    expression = str(concat),
    expression_type = "PYTHON3",
    code_block = "",
    field_type = "TEXT",
    enforce_domains = "NO_ENFORCE_DOMAINS"
  )
  i += 1

 

I am about to save each spot in the array to its own field ie array[0] to Field A, array[1] to Field B, array[2] to Field C. then Calculate the field as FieldA + "_" + FieldB + "_" + FieldC

However that will add run time and I have this looping through 2500 features

Thanks,
Brandon
0 Kudos
1 Solution

Accepted Solutions
BrandonMcAlister
Frequent Contributor

I have a solution!

I was sending the numbers through as a string but when the numbers were being sent through the calculate field script the did not have quotation marks attached to them.

I used the Geoprocessing pane to test. I sent the numbers through like this "1010_232_98.2" and it wrote correctly to the field with the underscores _ . I then use the Geoprocessing pace to send the numbers through like this 1010_232_98.2 and it wrote to the field without the underscores _.

I have since adjusted the code to look like this

parcels = "Parcels (Block and Lot) Data - For Internal Purposes Only - Do Not Distribute"
i = 0
cursor = arcpy.da.SearchCursor(Geocode, ["ObjectID"])
array = cursor._as_narray()
while i < len(array):
  arcpy.management.SelectLayerByAttribute(Geocode, where_clause = "ObjectID = " + str(array[i][0]))
  arcpy.management.SelectLayerByLocation(
    in_layer="'Parcels (Block and Lot) Data - For Internal Purposes Only - Do Not Distribute'",
    overlap_type="INTERSECT",
    select_features="CSRR_PI_Coords_Geocoded",
    search_distance=None,
    selection_type="NEW_SELECTION",
    invert_spatial_relationship="NOT_INVERT"
  )
  pcursor = arcpy.da.SearchCursor(parcels, ["Pams_Pin"])
  parray = pcursor._as_narray()
  string = parray[0][0]

  tada = ' " ' + string + ' " ' # This appends quotation marks to the numbers so it can be sent through the calculate field tool with " " attached

  arcpy.management.CalculateField(
    in_table=Geocode,
    field = "USER_parcel_data",
    expression = tada,
    expression_type = "PYTHON3",
    code_block = "",
    field_type = "TEXT",
    enforce_domains = "NO_ENFORCE_DOMAINS"
  )
  i += 1

Thanks,
Brandon

View solution in original post

0 Kudos
1 Reply
BrandonMcAlister
Frequent Contributor

I have a solution!

I was sending the numbers through as a string but when the numbers were being sent through the calculate field script the did not have quotation marks attached to them.

I used the Geoprocessing pane to test. I sent the numbers through like this "1010_232_98.2" and it wrote correctly to the field with the underscores _ . I then use the Geoprocessing pace to send the numbers through like this 1010_232_98.2 and it wrote to the field without the underscores _.

I have since adjusted the code to look like this

parcels = "Parcels (Block and Lot) Data - For Internal Purposes Only - Do Not Distribute"
i = 0
cursor = arcpy.da.SearchCursor(Geocode, ["ObjectID"])
array = cursor._as_narray()
while i < len(array):
  arcpy.management.SelectLayerByAttribute(Geocode, where_clause = "ObjectID = " + str(array[i][0]))
  arcpy.management.SelectLayerByLocation(
    in_layer="'Parcels (Block and Lot) Data - For Internal Purposes Only - Do Not Distribute'",
    overlap_type="INTERSECT",
    select_features="CSRR_PI_Coords_Geocoded",
    search_distance=None,
    selection_type="NEW_SELECTION",
    invert_spatial_relationship="NOT_INVERT"
  )
  pcursor = arcpy.da.SearchCursor(parcels, ["Pams_Pin"])
  parray = pcursor._as_narray()
  string = parray[0][0]

  tada = ' " ' + string + ' " ' # This appends quotation marks to the numbers so it can be sent through the calculate field tool with " " attached

  arcpy.management.CalculateField(
    in_table=Geocode,
    field = "USER_parcel_data",
    expression = tada,
    expression_type = "PYTHON3",
    code_block = "",
    field_type = "TEXT",
    enforce_domains = "NO_ENFORCE_DOMAINS"
  )
  i += 1

Thanks,
Brandon
0 Kudos