Trying to revise the placement of numbers and letters in a field

1968
12
08-10-2016 01:29 PM
jamesschoolar
New Contributor

I am new at Python. I have a dbf table with a text field.  Some of the records contain one or two digit numbers at the beginning followed by a string various capital letters which may then be followed by one or two digit numbers and another string of capital letters. Example: 12NESW3SESE. This is just one example of many combinations of numbers and letters. I need to change the order of the records so that the initial one or two digit number follows, rather than precedes, the letters until it gets to the second instance of a one or two digit number. Likewise, the second instance of one or two digit numbers needs to be moved to the end of the second sequence of capital letters. Also, in all cases a semicolon needs to precede the one or two digit number and a comma after the first set of one or two digit numbers and before the second set of capital letters. Hence, the example should be revised to NESW;12,SESE;3

0 Kudos
12 Replies
jamesschoolar
New Contributor

Dan, Thanks for the script and taking time to explain verbosely. I am making progress for sure with all this great help. I've got 370 thousand records to process on the first set and 13 sets to process. Such powerful coding. Bob

0 Kudos
DanPatterson_Retired
MVP Emeritus

let me know if you find any case where it doesn't work,  it can always be tweeked

0 Kudos
GeorgeNewbury
Occasional Contributor

If I had to tackle this I'd consider

1. How many records do I need to edit?

2. And I going to have to do this again and frequently?

3. Are all records the same length?

4. Do I need any numbers to be zfilled, e.g. 01 vs 1

5. Is Python the most appropriate tool for this?

6. Do I expect there to be any errors/inconsistencies in the existing data?

- If it's only a small number of records, e.g. less than 500 and you only need to do it this one time, then it may be easier to just edit it manually.

- If the records are all different lengths, or if you need to do z-filling, then that increases the complexity of the Python code

- If your or someone you know is good at Excel, then you may be able to use that. Also, if you are within the ArcGIS environment, you may consider just exploding the string into multiple columns, and then using field calculations to put them back as you want.

- if there are alot of errors in the data then you need to make sure the code can catch and alert you to the problem strings.

I saw in your last post that you've got 370k x 13 sets to process, so not all of this is applicable, but I think it is important to consider since these threads can be searched by someone years from now.

George