1

In ArcGIS I have intersected a large number of zonal polygons with another set and recorded the original zone IDs and the data they are connected with. However the strings that are created are one long list of numbers ranging from 11 to 77 (each ID is 11 characters long). I am looking to add a "," between each one making, it easier to read and export later as a .csv file. To do this I wrote this code:

def StringSplit(StrO,X):
  StrN = StrO #Recording original string 
  StrLen = len(StrN) 
  BStr = StrLen/X #How many segments are inside of one string
  StrC = BStr - 1 #How many times it should loop
  if StrC > 0:  
    while StrC > 1:
      StrN = StrN[ :((X * StrC) + 1)] + "," + StrN[(X * StrC): ]
      StrC = StrC - 1
    while StrC == 1:
      StrN = StrN[:X+1] + "," + StrN[(X*StrC):]
      StrC = 0 
    while StrC == 0:
      return StrN
  else:
    return StrN

The main issue is how it has to step through multiple rows (76) with various lengths (11 -> 77). I got the last parts to work, just not the internal loop as it returns an error or incorrect outputs for strings longer than 22 characters.

Thus right now:

1. 01234567890 returns 01234567890

2. 0123456789001234567890 returns 01234567890,01234567890

3. 012345678900123456789001234567890 returns either: Error or ,, or even ,,01234567890

I know it is probably something pretty simple I am missing, but I can't seem remember what it is...

2 Answers 2

1

It can be easily done by regex. those ........... are 11 dots for give split for every 11th char.

you can use pandas to create csv from the array output

Code:

import re

x = re.findall('...........', '01234567890012345678900123456789001234567890')

print(x)
myString = ",".join(x)

print(myString)

output:

['01234567890', '01234567890', '01234567890', '01234567890']
01234567890,01234567890,01234567890,01234567890

for the sake of simplicity you can do this

code:

x = ",".join(re.findall('...........', '01234567890012345678900123456789001234567890'))


print(x)
Sign up to request clarification or add additional context in comments.

4 Comments

When I attempt this solution in ArcGIS's python editor the import function, doesn't seem to be able to grab the "re" module. Just spitting back an error and crashing out the editor. At least not in the on the fly Pre-Logic Script Code, which is what I was attempting to solve the problem in first, before resorting to loading in an import of an import.
Thanks, that was helpful, though it is now reading it as a different type. As ArcGIS doesn't like same field concatenations so when it reads in a list of ['X', 'Y', 'Z'] it into a string valued line it doesn't know what to do with it. Hence why I need to do edit in a ",", while maintaining it as a single string ['X,Y,Z'].
Yep, that works now and so does Gelineau's solution. Thanks a lot for help! I am still getting slightly used to all of this as I am learning coding and programming as I go and half self taught. So my code can be a bit brutish at times.
1

Don't make the loops by yourself, use python libraries or builtins, it will be easier. For example :

def StringSplit(StrO,X):
    substring_starts = range(0, len(StrO), X)
    substrings = (StrO[start:start + X] for start in substring_starts)
    return ','.join(substrings)

string = '1234567890ABCDE'
print(StringSplit(string, 5))

# '12345,67890,ABCDE'

3 Comments

Gelineau, thanks for the solution as well. The code wasn't working at first, unsure why, but after a second try (most likely I copied something wrong on my end) it worked perfectly. As I am still slightly getting used programming (mostly self taught and for a modding) my code can be a bit of a brute force attempt rather than something elegant like this. Thanks again!
Happy to have helped you. If my answer helped you, you can upvote it :-)
Unfortunately I haven't reached 15 reputation, so although I have up voted your post it is not showing up - sorry.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.