Splitting string ID code into various partsArcMap Field Calculator syntax (again)Removing numeric characters from alphanumeric value in field calculation?Creating a new integer field based on text in a different field - ArcGIS 10.1How to replace null values in attribute table ArcGIS 10.2Arcmap record value is not displaying entirelyextract substring after first numberArcMap 10.3.1-Attribute Table Sorting IssuesSelect points by continuous numbersAddress Prefix Strip Using PythonSplitting ID number along hyphens and creating new field with only parts of ID In Field Calculator?
Hero deduces identity of a killer
The IT department bottlenecks progress. How should I handle this?
Has any country ever had 2 former presidents in jail simultaneously?
Why can Carol Danvers change her suit colours in the first place?
Moving brute-force search to FPGA
Why does AES have exactly 10 rounds for a 128-bit key, 12 for 192 bits and 14 for a 256-bit key size?
Creepy dinosaur pc game identification
Open a doc from terminal, but not by its name
Why is this estimator biased?
Does an advisor owe his/her student anything? Will an advisor keep a PhD student only out of pity?
What are the advantages of simplicial model categories over non-simplicial ones?
How do you make your own symbol when Detexify fails?
How do apertures which seem too large to physically fit work?
Why is it that I can sometimes guess the next note?
14 year old daughter buying thongs
photorec photo recovery software not seeing my mounted filesystem - trying to use photorec to recover lost jpegs
Why does the Sun have different day lengths, but not the gas giants?
On a tidally locked planet, would time be quantized?
Does Doodling or Improvising on the Piano Have Any Benefits?
Lowest total scrabble score
How do you respond to a colleague from another team when they're wrongly expecting that you'll help them?
A social experiment. What is the worst that can happen?
15% tax on $7.5k earnings. Is that right?
How does the math work for Perception checks?
Splitting string ID code into various parts
ArcMap Field Calculator syntax (again)Removing numeric characters from alphanumeric value in field calculation?Creating a new integer field based on text in a different field - ArcGIS 10.1How to replace null values in attribute table ArcGIS 10.2Arcmap record value is not displaying entirelyextract substring after first numberArcMap 10.3.1-Attribute Table Sorting IssuesSelect points by continuous numbersAddress Prefix Strip Using PythonSplitting ID number along hyphens and creating new field with only parts of ID In Field Calculator?
I have a series of identification codes that I need to split out. The format of these codes is [region(letter)][district(number)] - [place(number)][subdistrict(letter)]
.
An example of some codes includes S22-201
, TT100-12
, and V6-1B
. Often there is no subdistrict, and all points fall within the same larger district (so no As or Cs or whatever at the end of the string.
I can do parts of it, like splitting at the hyphen.
!Original_ID!.split('-')[0]
and then extracting the district
!Split_ID![1:3]
But it seems like two steps for this are unnecessary, and only works when I know the specific number of characters in the string, which isn't realistic for a large data set.
I'd like to be able to grab each piece at once:
- letters on the left of the hyphen
- numbers on the left of the hyphen
- numbers on the right of the hyphen
- letters (if any) on the right of the hyphen.
I'd need the numeric fields to be integers (or I guess possibly floats in some rare cases maybe).
I am still not doing something correctly. I may need to start smaller and brush up on my Python before I do this, I just assumed this would be a good place to start learning. Here's where I am at, in the Python window in ArcMap.
with arcpy.da.UpdateCursor("Wet_Sub",['Flag_ID','District','Split_ID']) as uCur:
for sRow in uCur:
OrigID = sRow[0].split('-')[0] # first element in the Original_ID
charRng = range(len(OrigID)) # a range to iterate over
Chars = ''
Numbers = ''
for Idx in charRng:
if OrigID[Idx].isnumeric():
Numbers += OrigID[Idx]
else:
chars += OrigID[Idx]
sRow[1] = float(Numbers)
sRow[2] = Chars
uCur.updateRow(sRow)
"Wet_Sub" and 'Flag_ID' are the names of the feature class and actual original field. I also tried to follow along with user2856's suggestion. It looks like I may need to be using both of those code blocks, one pasted into another, but I wasn't sure how to fit them together and what parts to change/remove (e.g. "etc... from code block above").
arcgis-desktop arcmap field-calculator python-parser
add a comment |
I have a series of identification codes that I need to split out. The format of these codes is [region(letter)][district(number)] - [place(number)][subdistrict(letter)]
.
An example of some codes includes S22-201
, TT100-12
, and V6-1B
. Often there is no subdistrict, and all points fall within the same larger district (so no As or Cs or whatever at the end of the string.
I can do parts of it, like splitting at the hyphen.
!Original_ID!.split('-')[0]
and then extracting the district
!Split_ID![1:3]
But it seems like two steps for this are unnecessary, and only works when I know the specific number of characters in the string, which isn't realistic for a large data set.
I'd like to be able to grab each piece at once:
- letters on the left of the hyphen
- numbers on the left of the hyphen
- numbers on the right of the hyphen
- letters (if any) on the right of the hyphen.
I'd need the numeric fields to be integers (or I guess possibly floats in some rare cases maybe).
I am still not doing something correctly. I may need to start smaller and brush up on my Python before I do this, I just assumed this would be a good place to start learning. Here's where I am at, in the Python window in ArcMap.
with arcpy.da.UpdateCursor("Wet_Sub",['Flag_ID','District','Split_ID']) as uCur:
for sRow in uCur:
OrigID = sRow[0].split('-')[0] # first element in the Original_ID
charRng = range(len(OrigID)) # a range to iterate over
Chars = ''
Numbers = ''
for Idx in charRng:
if OrigID[Idx].isnumeric():
Numbers += OrigID[Idx]
else:
chars += OrigID[Idx]
sRow[1] = float(Numbers)
sRow[2] = Chars
uCur.updateRow(sRow)
"Wet_Sub" and 'Flag_ID' are the names of the feature class and actual original field. I also tried to follow along with user2856's suggestion. It looks like I may need to be using both of those code blocks, one pasted into another, but I wasn't sure how to fit them together and what parts to change/remove (e.g. "etc... from code block above").
arcgis-desktop arcmap field-calculator python-parser
add a comment |
I have a series of identification codes that I need to split out. The format of these codes is [region(letter)][district(number)] - [place(number)][subdistrict(letter)]
.
An example of some codes includes S22-201
, TT100-12
, and V6-1B
. Often there is no subdistrict, and all points fall within the same larger district (so no As or Cs or whatever at the end of the string.
I can do parts of it, like splitting at the hyphen.
!Original_ID!.split('-')[0]
and then extracting the district
!Split_ID![1:3]
But it seems like two steps for this are unnecessary, and only works when I know the specific number of characters in the string, which isn't realistic for a large data set.
I'd like to be able to grab each piece at once:
- letters on the left of the hyphen
- numbers on the left of the hyphen
- numbers on the right of the hyphen
- letters (if any) on the right of the hyphen.
I'd need the numeric fields to be integers (or I guess possibly floats in some rare cases maybe).
I am still not doing something correctly. I may need to start smaller and brush up on my Python before I do this, I just assumed this would be a good place to start learning. Here's where I am at, in the Python window in ArcMap.
with arcpy.da.UpdateCursor("Wet_Sub",['Flag_ID','District','Split_ID']) as uCur:
for sRow in uCur:
OrigID = sRow[0].split('-')[0] # first element in the Original_ID
charRng = range(len(OrigID)) # a range to iterate over
Chars = ''
Numbers = ''
for Idx in charRng:
if OrigID[Idx].isnumeric():
Numbers += OrigID[Idx]
else:
chars += OrigID[Idx]
sRow[1] = float(Numbers)
sRow[2] = Chars
uCur.updateRow(sRow)
"Wet_Sub" and 'Flag_ID' are the names of the feature class and actual original field. I also tried to follow along with user2856's suggestion. It looks like I may need to be using both of those code blocks, one pasted into another, but I wasn't sure how to fit them together and what parts to change/remove (e.g. "etc... from code block above").
arcgis-desktop arcmap field-calculator python-parser
I have a series of identification codes that I need to split out. The format of these codes is [region(letter)][district(number)] - [place(number)][subdistrict(letter)]
.
An example of some codes includes S22-201
, TT100-12
, and V6-1B
. Often there is no subdistrict, and all points fall within the same larger district (so no As or Cs or whatever at the end of the string.
I can do parts of it, like splitting at the hyphen.
!Original_ID!.split('-')[0]
and then extracting the district
!Split_ID![1:3]
But it seems like two steps for this are unnecessary, and only works when I know the specific number of characters in the string, which isn't realistic for a large data set.
I'd like to be able to grab each piece at once:
- letters on the left of the hyphen
- numbers on the left of the hyphen
- numbers on the right of the hyphen
- letters (if any) on the right of the hyphen.
I'd need the numeric fields to be integers (or I guess possibly floats in some rare cases maybe).
I am still not doing something correctly. I may need to start smaller and brush up on my Python before I do this, I just assumed this would be a good place to start learning. Here's where I am at, in the Python window in ArcMap.
with arcpy.da.UpdateCursor("Wet_Sub",['Flag_ID','District','Split_ID']) as uCur:
for sRow in uCur:
OrigID = sRow[0].split('-')[0] # first element in the Original_ID
charRng = range(len(OrigID)) # a range to iterate over
Chars = ''
Numbers = ''
for Idx in charRng:
if OrigID[Idx].isnumeric():
Numbers += OrigID[Idx]
else:
chars += OrigID[Idx]
sRow[1] = float(Numbers)
sRow[2] = Chars
uCur.updateRow(sRow)
"Wet_Sub" and 'Flag_ID' are the names of the feature class and actual original field. I also tried to follow along with user2856's suggestion. It looks like I may need to be using both of those code blocks, one pasted into another, but I wasn't sure how to fit them together and what parts to change/remove (e.g. "etc... from code block above").
arcgis-desktop arcmap field-calculator python-parser
arcgis-desktop arcmap field-calculator python-parser
edited Mar 15 at 20:05
PolyGeo♦
53.7k1781244
53.7k1781244
asked Mar 15 at 1:12
vce500vce500
42
42
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
You're not going to be able to calculate two fields in one go.. though you can split it up into two calcs. I would do this with an update cursor:
with arcpy.da.UpdateCursor(YourFeatureClass,['Original_ID','District','Split_ID']) as uCur:
for sRow in uCur:
OrigID = sRow[0].split('-')[0] # first element in the Original_ID
charRng = range(len(OrigID)) # a range to iterate over
Chars = ''
Numbers = ''
for Idx in charRng:
if OrigID[Idx].isnumeric():
Numbers += OrigID[Idx]
else:
chars += OrigID[Idx]
sRow[1] = float(Numbers)
sRow[2] = Chars
uCur.updateRow(sRow)
This shows how to break up a string into numbers and not numbers and put the values into a row, it should give you some ideas where to start from.
add a comment |
Assuming you have four fields, region, district, place and subdistrict already added and you want to use the field calculator to populate them. You would have to run the calculator four times using an expression like:
Code Block
import re
def parse(s):
"""The format of these codes is [region(letter)][district(number)] - [place(number)][subdistrict(letter)].
An example of a some codes include S22-201, TT100-12, and V6-1B.
Often there is no subdistrict, and all points fall within the same larger district
(so no As or Cs or whatever at the end of the string)."""
letters = re.findall(r'[a-z A-Z]+', s)
numbers = re.findall(r'[0-9]+', s)
region = letters[0]
district, place = [int(n) for n in numbers]
try:
subdistrict = letters[1]
except IndexError:
subdistrict = None
return region, district, place, subdistrict
Then for the region field, use:
parse(!Original_ID!)[0]
For district:
parse(!Original_ID!)[1]
For place:
parse(!Original_ID!)[2]
For subdistrict:
parse(!Original_ID!)[3]
However, I would use the update cursor approach suggested by Michael Stimson so you could update all four fields in one hit. Use the following in the python window of ArcMap/ArcGIS Pro:
import re
def parse(s):
etc... from code block above
with arcpy.da.UpdateCursor(YourFeatureClass, ['Original_ID','Region', 'District', 'Place', 'Subdistrict']) as rows:
for row in rows:
region, district, place, subdistrict = parse(row[0])
row = [row[0], region, district, place, subdistrict]
rows.updateRow(row)
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "79"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f315591%2fsplitting-string-id-code-into-various-parts%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You're not going to be able to calculate two fields in one go.. though you can split it up into two calcs. I would do this with an update cursor:
with arcpy.da.UpdateCursor(YourFeatureClass,['Original_ID','District','Split_ID']) as uCur:
for sRow in uCur:
OrigID = sRow[0].split('-')[0] # first element in the Original_ID
charRng = range(len(OrigID)) # a range to iterate over
Chars = ''
Numbers = ''
for Idx in charRng:
if OrigID[Idx].isnumeric():
Numbers += OrigID[Idx]
else:
chars += OrigID[Idx]
sRow[1] = float(Numbers)
sRow[2] = Chars
uCur.updateRow(sRow)
This shows how to break up a string into numbers and not numbers and put the values into a row, it should give you some ideas where to start from.
add a comment |
You're not going to be able to calculate two fields in one go.. though you can split it up into two calcs. I would do this with an update cursor:
with arcpy.da.UpdateCursor(YourFeatureClass,['Original_ID','District','Split_ID']) as uCur:
for sRow in uCur:
OrigID = sRow[0].split('-')[0] # first element in the Original_ID
charRng = range(len(OrigID)) # a range to iterate over
Chars = ''
Numbers = ''
for Idx in charRng:
if OrigID[Idx].isnumeric():
Numbers += OrigID[Idx]
else:
chars += OrigID[Idx]
sRow[1] = float(Numbers)
sRow[2] = Chars
uCur.updateRow(sRow)
This shows how to break up a string into numbers and not numbers and put the values into a row, it should give you some ideas where to start from.
add a comment |
You're not going to be able to calculate two fields in one go.. though you can split it up into two calcs. I would do this with an update cursor:
with arcpy.da.UpdateCursor(YourFeatureClass,['Original_ID','District','Split_ID']) as uCur:
for sRow in uCur:
OrigID = sRow[0].split('-')[0] # first element in the Original_ID
charRng = range(len(OrigID)) # a range to iterate over
Chars = ''
Numbers = ''
for Idx in charRng:
if OrigID[Idx].isnumeric():
Numbers += OrigID[Idx]
else:
chars += OrigID[Idx]
sRow[1] = float(Numbers)
sRow[2] = Chars
uCur.updateRow(sRow)
This shows how to break up a string into numbers and not numbers and put the values into a row, it should give you some ideas where to start from.
You're not going to be able to calculate two fields in one go.. though you can split it up into two calcs. I would do this with an update cursor:
with arcpy.da.UpdateCursor(YourFeatureClass,['Original_ID','District','Split_ID']) as uCur:
for sRow in uCur:
OrigID = sRow[0].split('-')[0] # first element in the Original_ID
charRng = range(len(OrigID)) # a range to iterate over
Chars = ''
Numbers = ''
for Idx in charRng:
if OrigID[Idx].isnumeric():
Numbers += OrigID[Idx]
else:
chars += OrigID[Idx]
sRow[1] = float(Numbers)
sRow[2] = Chars
uCur.updateRow(sRow)
This shows how to break up a string into numbers and not numbers and put the values into a row, it should give you some ideas where to start from.
answered Mar 15 at 1:35
Michael StimsonMichael Stimson
21.6k22460
21.6k22460
add a comment |
add a comment |
Assuming you have four fields, region, district, place and subdistrict already added and you want to use the field calculator to populate them. You would have to run the calculator four times using an expression like:
Code Block
import re
def parse(s):
"""The format of these codes is [region(letter)][district(number)] - [place(number)][subdistrict(letter)].
An example of a some codes include S22-201, TT100-12, and V6-1B.
Often there is no subdistrict, and all points fall within the same larger district
(so no As or Cs or whatever at the end of the string)."""
letters = re.findall(r'[a-z A-Z]+', s)
numbers = re.findall(r'[0-9]+', s)
region = letters[0]
district, place = [int(n) for n in numbers]
try:
subdistrict = letters[1]
except IndexError:
subdistrict = None
return region, district, place, subdistrict
Then for the region field, use:
parse(!Original_ID!)[0]
For district:
parse(!Original_ID!)[1]
For place:
parse(!Original_ID!)[2]
For subdistrict:
parse(!Original_ID!)[3]
However, I would use the update cursor approach suggested by Michael Stimson so you could update all four fields in one hit. Use the following in the python window of ArcMap/ArcGIS Pro:
import re
def parse(s):
etc... from code block above
with arcpy.da.UpdateCursor(YourFeatureClass, ['Original_ID','Region', 'District', 'Place', 'Subdistrict']) as rows:
for row in rows:
region, district, place, subdistrict = parse(row[0])
row = [row[0], region, district, place, subdistrict]
rows.updateRow(row)
add a comment |
Assuming you have four fields, region, district, place and subdistrict already added and you want to use the field calculator to populate them. You would have to run the calculator four times using an expression like:
Code Block
import re
def parse(s):
"""The format of these codes is [region(letter)][district(number)] - [place(number)][subdistrict(letter)].
An example of a some codes include S22-201, TT100-12, and V6-1B.
Often there is no subdistrict, and all points fall within the same larger district
(so no As or Cs or whatever at the end of the string)."""
letters = re.findall(r'[a-z A-Z]+', s)
numbers = re.findall(r'[0-9]+', s)
region = letters[0]
district, place = [int(n) for n in numbers]
try:
subdistrict = letters[1]
except IndexError:
subdistrict = None
return region, district, place, subdistrict
Then for the region field, use:
parse(!Original_ID!)[0]
For district:
parse(!Original_ID!)[1]
For place:
parse(!Original_ID!)[2]
For subdistrict:
parse(!Original_ID!)[3]
However, I would use the update cursor approach suggested by Michael Stimson so you could update all four fields in one hit. Use the following in the python window of ArcMap/ArcGIS Pro:
import re
def parse(s):
etc... from code block above
with arcpy.da.UpdateCursor(YourFeatureClass, ['Original_ID','Region', 'District', 'Place', 'Subdistrict']) as rows:
for row in rows:
region, district, place, subdistrict = parse(row[0])
row = [row[0], region, district, place, subdistrict]
rows.updateRow(row)
add a comment |
Assuming you have four fields, region, district, place and subdistrict already added and you want to use the field calculator to populate them. You would have to run the calculator four times using an expression like:
Code Block
import re
def parse(s):
"""The format of these codes is [region(letter)][district(number)] - [place(number)][subdistrict(letter)].
An example of a some codes include S22-201, TT100-12, and V6-1B.
Often there is no subdistrict, and all points fall within the same larger district
(so no As or Cs or whatever at the end of the string)."""
letters = re.findall(r'[a-z A-Z]+', s)
numbers = re.findall(r'[0-9]+', s)
region = letters[0]
district, place = [int(n) for n in numbers]
try:
subdistrict = letters[1]
except IndexError:
subdistrict = None
return region, district, place, subdistrict
Then for the region field, use:
parse(!Original_ID!)[0]
For district:
parse(!Original_ID!)[1]
For place:
parse(!Original_ID!)[2]
For subdistrict:
parse(!Original_ID!)[3]
However, I would use the update cursor approach suggested by Michael Stimson so you could update all four fields in one hit. Use the following in the python window of ArcMap/ArcGIS Pro:
import re
def parse(s):
etc... from code block above
with arcpy.da.UpdateCursor(YourFeatureClass, ['Original_ID','Region', 'District', 'Place', 'Subdistrict']) as rows:
for row in rows:
region, district, place, subdistrict = parse(row[0])
row = [row[0], region, district, place, subdistrict]
rows.updateRow(row)
Assuming you have four fields, region, district, place and subdistrict already added and you want to use the field calculator to populate them. You would have to run the calculator four times using an expression like:
Code Block
import re
def parse(s):
"""The format of these codes is [region(letter)][district(number)] - [place(number)][subdistrict(letter)].
An example of a some codes include S22-201, TT100-12, and V6-1B.
Often there is no subdistrict, and all points fall within the same larger district
(so no As or Cs or whatever at the end of the string)."""
letters = re.findall(r'[a-z A-Z]+', s)
numbers = re.findall(r'[0-9]+', s)
region = letters[0]
district, place = [int(n) for n in numbers]
try:
subdistrict = letters[1]
except IndexError:
subdistrict = None
return region, district, place, subdistrict
Then for the region field, use:
parse(!Original_ID!)[0]
For district:
parse(!Original_ID!)[1]
For place:
parse(!Original_ID!)[2]
For subdistrict:
parse(!Original_ID!)[3]
However, I would use the update cursor approach suggested by Michael Stimson so you could update all four fields in one hit. Use the following in the python window of ArcMap/ArcGIS Pro:
import re
def parse(s):
etc... from code block above
with arcpy.da.UpdateCursor(YourFeatureClass, ['Original_ID','Region', 'District', 'Place', 'Subdistrict']) as rows:
for row in rows:
region, district, place, subdistrict = parse(row[0])
row = [row[0], region, district, place, subdistrict]
rows.updateRow(row)
edited Mar 15 at 4:23
answered Mar 15 at 4:03
user2856user2856
30.5k258106
30.5k258106
add a comment |
add a comment |
Thanks for contributing an answer to Geographic Information Systems Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f315591%2fsplitting-string-id-code-into-various-parts%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown