Ok, I'm quite new to Python
Bengt Richter
bokr at oz.net
Wed Oct 13 01:33:09 EDT 2004
On Wed, 13 Oct 2004 00:03:41 +0000 (UTC), "Michael" <slick_mick_00 at hotmail.com> wrote:
>But i'm a good c++ programmer.
>
>What i want to do is parse a text file and store the information in relevant
^^^^^^^^^^^^^^^^^[1] ^^^^^[2] ^^^^^^^^^^^[3] ^^^^^^^^-
>fields:
-^^^^^[4]
[1] ok
[2] where?
[3] which
[4] relevant to what?
[5] ;-)
>
>//Text File:
>
>*Version 200
>*SCENE {
> AMBIENT_COLOUR 0.0 0.0 0.0
> }
>
>*MATERIAL_LIST{
> *MATERIAL_COUNT 0
> }
>*GEOMOBJECT {
> *NODE_NAME "obj1"
> *MESH {
> *MESH_VERTEX_LIST{
> *MESH_VERTEX 0 0 0 0
> *MESH_VERTEX 1 0 1 2
> }
> *MESH_FACE_LIST {
> *MESH_FACE 1 2 3
> }
> }
> }
>/* ... More GEOMOBJECTS ...*/
>
>
>but I have no idea what the best way to do this is?
^^^^^^^[1]
[1] do what?
>Any thoughts??
>
Id probably start eith stripping out the tokens with a regular expression
and then process the list to build a tree that you can then walk? To start:
>>> data = """\
... *Version 200
... *SCENE {
... AMBIENT_COLOUR 0.0 0.0 0.0
... }
...
... *MATERIAL_LIST{
... *MATERIAL_COUNT 0
... }
... *GEOMOBJECT {
... *NODE_NAME "obj1"
... *MESH {
... *MESH_VERTEX_LIST{
... *MESH_VERTEX 0 0 0 0
... *MESH_VERTEX 1 0 1 2
... }
... *MESH_FACE_LIST {
... *MESH_FACE 1 2 3
... }
... }
... }
... """
>>> import re
>>> rxs = re.compile(r'([{}]|"[^"]*"|[*A-Z_a-z]+|[0-9.]+)')
>>> tokens = rxs.findall(data)
>>> tokens
['*Version', '200', '*SCENE', '{', 'AMBIENT_COLOUR', '0.0', '0.0', '0.0', '}', '*MATERIAL_LIST',
'{', '*MATERIAL_COUNT', '0', '}', '*GEOMOBJECT', '{', '*NODE_NAME', '"obj1"', '*MESH', '{', '*M
ESH_VERTEX_LIST', '{', '*MESH_VERTEX', '0', '0', '0', '0', '*MESH_VERTEX', '1', '0', '1', '2', '
}', '*MESH_FACE_LIST', '{', '*MESH_FACE', '1', '2', '3', '}', '}', '}']
IWT that isolates the basic info of interest. It should not be hard to make a tree or
extract what suits your purposes, but I'm not going to guess what those are ;-)
Regards,
Bengt Richter
More information about the Python-list
mailing list