> So i'm working on a view that will need to parse character > strings that will have a predictable structure, but dynamic > content. The format will look like > "%Zxxxxxxxxxxxxxx^xxxxxxxxxx?;xxxxxxxxxxxxxx? where all x are > the unpredictable characters and all the rest will be present > in EVERY string, regardless of content. I'm relatively new to > django, so there are many functions, and options that i'm > unaware of, and i'm sure there is some type of split() > modifier, or some related function that would help in parsing > the text for use as variety of variables derived from the > original string.
This sounds like a common use for regular expressions: >>> s = "%Zxxxxxxxxxxxxxx^xxxxxxxxxx?;xxxxxxxxxxxxxx?" >>> import re >>> r = re.compile(r"%Z([^^]*)\^([^?]*)\?;([^?]*)\?") >>> m = r.match(s) >>> m.groups() ('xxxxxxxxxxxxxx', 'xxxxxxxxxx', 'xxxxxxxxxxxxxx') >>> a, b, c = m.groups() It gets a little confusing to parse that regular expression because of all the escaping but it's %Z literal "%Z" (...) first grouping [^^]* zero or more characters that aren't a "^" \^ a literal "^" (...) second grouping [^?]* zero or more characters that aren't a "?" \?; a literal "?;" (...) third grouping [^?]* zero or more characters that aren't a "?" \? a literal "?" If you need the transform in a view, you'd need to create a custom filter (see the online help about creating your own filters) > Also, does anyone happen to know of some command within django > that will allow me to capture a specific number of leading > characters? Something like foo = > original_string.MAGICFUNCTION(4) that would capture the first > 4 characters and save them to foo would be awesome. that's easy: >>> original_string = "this is a test" >>> original_string[:4] 'this' Django's template language already contains a "slice" filter {{ original_string|slice:":4" }} -tim --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---