-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSeparate-Text
76 lines (57 loc) · 2.56 KB
/
Separate-Text
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#make a new column with the text after "-" for attribute table data formatted as "Figure 1 - County Road 749"
import arcpy
arcpy.env.workspace = r"M:\Projects_Working\Platte_Elwood\GIS\Feature_Data\PhotoMap\PhotoPoints.gdb"
feature_class = "Points"
field_name = "Name"
new_field_name = "Description"
arcpy.AddField_management(feature_class, new_field_name, "TEXT")
# Update the new field with the text after the hyphen
with arcpy.da.UpdateCursor(feature_class, [field_name, new_field_name]) as cursor:
for row in cursor:
name = row[0]
if "-" in name:
text_after_hyphen = name.split("-")[1].strip()
row[1] = text_after_hyphen
cursor.updateRow(row)
print("Text after hyphen extracted and populated into the new column successfully.")
## use below if you want to then remove the split text from the first column
## RUN IN NEW COMMAND
import arcpy
arcpy.env.workspace = r"M:\Projects_Working\Platte_Elwood\GIS\Feature_Data\PhotoMap\PhotoPoints.gdb"
# Set the name of the feature class and the field you want to work with
feature_class = "Points"
field_name = "Name"
# Update the original "Name" field to remove text starting from the hyphen
with arcpy.da.UpdateCursor(feature_class, [field_name]) as cursor:
for row in cursor:
name = row[0]
if "-" in name:
text_before_hyphen = name.split("-")[0].strip()
row[0] = text_before_hyphen
cursor.updateRow(row)
print("Text before hyphen extracted successfully.")
Text before hyphen extracted successfully.
import arcpy
# Set your workspace environment
arcpy.env.workspace = r"M:\Projects_Working\Platte_Elwood\GIS\Feature_Data\PhotoMap\PhotoPoints.gdb"
# Set the name of the feature class and the field you want to work with
feature_class = "Points"
field_name = "Name"
# Add a new field to hold the modified "Name" values
new_field_name = "Figure"
arcpy.AddField_management(feature_class, new_field_name, "TEXT")
# Update the new field with the modified "Name" values
with arcpy.da.UpdateCursor(feature_class, [field_name, new_field_name]) as cursor:
for row in cursor:
name = row[0]
if "-" in name:
text_before_hyphen = name.split("-")[0].strip()
row[1] = text_before_hyphen
else:
row[1] = name
cursor.updateRow(row)
# Delete the original "Name" field
arcpy.DeleteField_management(feature_class, field_name)
# Rename the new field to "Name"
arcpy.AlterField_management(feature_class, new_field_name, field_name)
print("Text before hyphen extracted and updated successfully.")