0

I receive via POST an unicode string like this:

u'[<Word: colors>, <Word: red>, <Word: blue>, <Word: yellow>, <Word: green>, <Word: orange>, <Word: purple>, <Word: brown>, <Word: white>, <Word: black>, <Word: grey>]'

I want it to be an array or a dictionary so I can work with it.

How can I do this?

Thanks.

4
  • String? "[<Word: colors>, <Word: red>, <Word: blue>, <Word: yellow>, <Word: green>, <Word: orange>, <Word: purple>, <Word: brown>, <Word: white>, <Word: black>, <Word: grey>] " Commented Jan 15, 2014 at 10:18
  • Yes, it was originally a list of objects, but, after going through HTML it comes back like that, but being an unicode string Commented Jan 15, 2014 at 10:20
  • 1
    I think you should look at it from the other end, and make it so that the string you receive in POST is something easier to work with. Commented Jan 15, 2014 at 10:32
  • Well, now that you say it you are right. Don't know why didn't I think of it Commented Jan 15, 2014 at 10:53

2 Answers 2

2

You should use a module for parsing a structured text, for example, pyparsing. Basically the grammar should look like this:

import pyparsing as pp

s = u'[<Word: colors>, <Word: red>, <Word: blue>, <Word: yellow>, <Word: green>, <Word: orange>, <Word: purple>, <Word: brown>, <Word: white>, <Word: black>, <Word: grey>]'

term = pp.Literal('<') + pp.Literal('Word') + pp.Literal(':') + pp.Word(pp.alphas) + pp.Literal('>')
expr = pp.Literal('[') + term + pp.ZeroOrMore( pp.Literal(',') + term ) + pp.Literal(']')

r = expr.parseString(s)

and then retrieve parse results from r. Check examples on the project site. Probably you'll need to set up specific parser callbacks on the items you wish to extract using setParseAction():

import pyparsing as pp

s = u'[<Word: colors>, <Word: red>, <Word: blue>, <Word: yellow>, <Word: green>, <Word: orange>, <Word: purple>, <Word: brown>, <Word: white>, <Word: black>, <Word: grey>]'

colors = []

term = pp.Literal('<') + pp.Literal('Word') + pp.Literal(':') + pp.Word(pp.alphas).setParseAction(lambda s: colors.append(s[0])) + pp.Literal('>')
expr = pp.Literal('[') + term + pp.ZeroOrMore( pp.Literal(',') + term ) + pp.Literal(']')

r = expr.parseString(s)

now colors contains the list of colors and so on...

Sign up to request clarification or add additional context in comments.

Comments

0

I think there must be a way to change the format of your data (because as I see you try to output list of python objects to form field what is not good idea).

Without more information about your code I can offer this:

output to form field 'colors,red,blue,yellow...'

then after post: list_of_values = input.split(',') (where input is recieved string from post)

Also you can use this code based what output you need

print map(lambda val: val.strip(' <>'), s.strip('[]').split(','))
print map(lambda val: val.strip(' <>').split(':')[1].strip(), s.strip('[]').split(','))

Also you can serialize or pickle data.

4 Comments

This approach will break if elements contain "bad characters" like , < > etc
@user3159253, yep! This is only a variant of solution and of course it does not covers all possible variants of data. The best way IMHO is to change format of post data.
Well, as I mentioned above, structured text (especially with such a simple grammar) should be parsed as, well, a structured text, not just a sequence of symbols. Explicitly written grammar would save you from hours of debugging if things eventually change. Any of "let's split it by delimiters" or "let's use a simple RE" approach doesn't survive a long run.
@user3159253, I agree with you but also I think that you should build your programm as simple as possible. And use less dependences as possible. So I offer variant with serialization which use builtin tools of language if you realy want to send structure to form, but I think that there must be way to avoid this.

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.