Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I converted a kml file to a feature class. All of the fields from the kml were combined into two fields in the feature class. One of them contains information delimited by "/" and the other contains information delimited by "br". I need to extract all of the information into separate fields. I met with some luck using the methods described here:

ArcGIS 10 Python split string

The problem is that the nothing is consistent about the information in the fields except for the delimiter. Some rows only contain one piece of information (no delimiter) and others contain up to five. When I use .split with a [0] to return the first piece of information it works fine, when I try to return the second piece [1] I get an error. If I get rid of the data that does not contain a delimiter then [1] works, but I don't know of a way to get rid of all of it.

I am very new to ArcMap, and I am aware that I'm probably not using the correct terminology. I have read a lot of forums, looked at python dictionaries ( for example: http://docs.python.org/library/string.html) and tried to make this work over the course of many hours. The bottom line is that I still don't understand Python well enough to come up with something that works on my own! Please help me!! I tried to post images showing what I have tried and what my data is like, but I can't because I am a new user. If you think you have any ideas I would love to share more info. Thanks!

share|improve this question
    
You should provide some examples of the strings you are splitting and the fields you are splitting them into. –  blord-castillo Aug 7 '12 at 17:23
add comment

1 Answer

up vote 4 down vote accepted

You can always force a list to a certain length:

k = "a/b/c"

l = "a/b/c/d"

def fill_to_length(split_string, delimiter, min_length):
    split_list = split_string.split(delimiter)
    split_list += [""] * (min_length - len(split_list))
    return split_list

# ['a', 'b', 'c']
fill_to_length(k, "/", 2)

# ['a', 'b', 'c', '', '', '', '']
fill_to_length(k, "/", 7)

# ['a', 'b', 'c', '', '']
fill_to_length(k, "/", 5)

This type of question is general enough that you can usually get a much better response on Stack Overflow.

share|improve this answer
1  
+1 Out of curiosity, what does Python do when len(split_list) > min_length? Another approach (which works in many languages) is just to append a sequence of delimiters, like "/////", to the argument before splitting it: that will guarantee a sufficiently long result regardless of the argument's value. –  whuber Aug 7 '12 at 18:26
2  
Try it! [5] * -5 -> [] you get nothing! So in this instance, the function guarantees a list of at least min_length, but it can be bigger –  Jason Scheirer Aug 7 '12 at 18:41
    
Thank you so much for the responses! The simple solution of adding a sequence of delimiters "//////" to the end of the argument worked perfectly. –  Rachel Aug 7 '12 at 19:44
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.