I'm looking at this library, which has little documentation: https://pythonhosted.org/parsec/#examples
I understand there are alternatives, but I'd like to use this library.
I have the following string I'd like to parse:
mystr = """
<kv>
key1: "string"
key2: 1.00005
key3: [1,2,3]
</kv>
<csv>
date,windspeed,direction
20190805,22,NNW
20190805,23,NW
20190805,20,NE
</csv>"""
While I'd like to parse the whole thing, I'd settle for just grabbing the <tags>
. I have:
>>> import parsec
>>> tag_start = parsec.Parser(lambda x: x == "<")
>>> tag_end = parsec.Parser(lambda x: x == ">")
>>> tag_name = parsec.Parser(parsec.Parser.compose(parsec.many1, parsec.letter))
>>> tag_open = parsec.Parser(parsec.Parser.joint(tag_start, tag_name, tag_end))
OK, looks good. Now to use it:
>>> tag_open.parse(mystr)
Traceback (most recent call last):
...
TypeError: <lambda>() takes 1 positional argument but 2 were given
This fails. I'm afraid I don't even understand what it meant about my lambda expression giving two arguments, it's clearly 1. How can I proceed?
My optimal desired output for all the bonus points is:
[
{"type": "tag",
"name" : "kv",
"values" : [
{"key1" : "string"},
{"key2" : 1.00005},
{"key3" : [1,2,3]}
]
},
{"type" : "tag",
"name" : "csv",
"values" : [
{"date" : 20190805, "windspeed" : 22, "direction": "NNW"}
{"date" : 20190805, "windspeed" : 23, "direction": "NW"}
{"date" : 20190805, "windspeed" : 20, "direction": "NE"}
]
}
The output I'd settle for understanding in this question is using functions like those described above for start and end tags to generate:
[
{"tag": "kv"},
{"tag" : "csv"}
]
And simply be able to parse arbitrary xml-like tags out of the messy mixed text entry.
Value.success()
orValue.failure()
as mentioned here. – Resemblancefn
must accept two arguments. It is passed the entire text and current index. – Inaugural