Reading Formatted Text File

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kevin McBrearty

    #1

    Reading Formatted Text File

    Hello All,

    I'm trying to read a formatted text of strings and
    floats. I have looked through previous posts and
    couldn't deciper a good method. I'm new to python so
    any suggestions would be helpful.

    Regards,
    Kevin

    solid SIMPLEBLOCK
    facet normal 0.000000e+00 0.000000e+00 -1.000000e+00
    outer loop
    vertex 1.000000e+00 -1.000000e+00 0.000000e+00
    vertex -1.000000e+00 -1.000000e+00 0.000000e+00
    vertex -1.000000e+00 1.000000e+00 0.000000e+00
    endloop
    endfacet



    _______________ _______________ ____
    Do you Yahoo!?
    The all-new My Yahoo! - Get yours free!
    Latest news coverage, email, free stock quotes, live scores and video are just the beginning. Discover more every day at Yahoo!



  • Kent Johnson

    #2
    Re: Reading Formatted Text File

    Kevin McBrearty wrote:[color=blue]
    > Hello All,
    >
    > I'm trying to read a formatted text of strings and
    > floats. I have looked through previous posts and
    > couldn't deciper a good method. I'm new to python so
    > any suggestions would be helpful.[/color]

    Here is a solution using pyparsing (http://pyparsing.sourceforge.net).
    The result is a nested list structure containing the original data in a
    structured form
    blocktype
    list of facets
    normal vector
    list of vertices

    Kent


    from pyparsing import *
    import string

    point = Literal( "." )
    e = CaselessLiteral ( "E" )
    fnumber = Combine( Word( "+-"+nums, nums ) +
    Optional( point + Optional( Word( nums ) ) ) +
    Optional( e + Word( "+-"+nums, nums ) ) )
    fnumber.setPars eAction( lambda s,l,t: [ float(t[0]) ] )

    triple = Group(fnumber + fnumber + fnumber)

    normal = Suppress('norma l') + triple
    vertex = Suppress('verte x') + triple

    facet = Suppress('facet ') + normal + Suppress('outer loop') +
    Group(OneOrMore (vertex)) + Suppress('endlo op')

    blockType = Word(string.upp ercase)

    solid = Suppress('solid ') + blockType + Group(OneOrMore (facet))

    data = '''
    solid SIMPLEBLOCK
    facet normal 0.000000e+00 0.000000e+00 -1.000000e+00
    outer loop
    vertex 1.000000e+00 -1.000000e+00 0.000000e+00
    vertex -1.000000e+00 -1.000000e+00 0.000000e+00
    vertex -1.000000e+00 1.000000e+00 0.000000e+00
    endloop
    endfacet
    '''

    print solid.parseStri ng(data)


    prints:
    ['SIMPLEBLOCK', [[0.0, 0.0, -1.0], [[1.0, -1.0, 0.0], [-1.0, -1.0, 0.0],
    [-1.0, 1.0, 0.0]]]]
    [color=blue]
    >
    > Regards,
    > Kevin
    >
    > solid SIMPLEBLOCK
    > facet normal 0.000000e+00 0.000000e+00 -1.000000e+00
    > outer loop
    > vertex 1.000000e+00 -1.000000e+00 0.000000e+00
    > vertex -1.000000e+00 -1.000000e+00 0.000000e+00
    > vertex -1.000000e+00 1.000000e+00 0.000000e+00
    > endloop
    > endfacet
    >
    >
    >
    > _______________ _______________ ____
    > Do you Yahoo!?
    > The all-new My Yahoo! - Get yours free!
    > http://my.yahoo.com
    >
    >[/color]

    Comment

    Working...