473,809 Members | 2,742 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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!
http://my.yahoo.com
Jul 18 '05 #1
1 2616
Kevin McBrearty wrote:
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.
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]]]]

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

Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1277
by: rodchar | last post by:
hey all, is there a quick way to read.all the contents of a text file, search for the text, and if it finds that text to read the entire line? thanks, rodchar
1
3406
by: mart2006 | last post by:
Hi, I'm currently reading a text file via PHP which, in itself, is very easy. However I want to specifically get one word from the text file and assign it as a variable and I'm struggling like mad! The text file is just a ping result, as follows: Pinging www.l.google.com with 32 bytes of data: Reply from 64.233.183.147: bytes=32 time=34ms TTL=249 Reply from 64.233.183.147: bytes=32 time=32ms TTL=248 Reply from 64.233.183.147:...
5
8794
by: Z.K. | last post by:
In C#, using the StreamReader, how do I detect when you get to the end of line. I am reading a text file using the Read() function and I need to detect the \n\r, but everything I try does not work. I am sure that this probably fairly simple, but I have not been able to figure it out. Z.K.
2
2280
by: bambataa | last post by:
Hellloww...i am new to Java and i am facing a problem, i am reading a text file, i am using string tokenizer class,..now at some point when i read a first token i have to check it if its an integer then i should read the line, else i should go to the next line...how can i do this.. thanx alot in advance
1
1796
by: engggirl3000 | last post by:
Another question I have, what is the difference between reading a text file to a program and opening a text file in the program? A sample of one of the text files is formatted like this: 3 Jon Smith 20009 40 32 12 13 14 17 12 Wilma Vohn 76448 20 30 12 12 18 20 14
0
1653
by: PRITPAL | last post by:
Hi There, I want a code for Saving and Reading formatted text (RTF File) in MS Access using ole Objects, i want to save 20 such records in DB using VB 6.0. Plz Help
9
7716
by: NEWSGROUPS | last post by:
I have data in a table in an Access 2000 database that needs to be exported to a formatted text file. For instance, the first field is an account number that is formatted in the table as text and is 8 characters long. This field needs to be exported as pic(15) padded in the front with 0's (zeros). The next field an ID name that is 15 characters that needs to be exported as pic(20) padded with trailing spaces. There are about 5 fields in...
2
1498
by: thanawala27 | last post by:
Hi, I'm facign a strange problem in reading a text file. The contents of my text file is: A1;B1;C1;D1 A2;B2;C2;D2 A3;B3;C3;D3
2
1994
by: friend.blah | last post by:
i have a text file lets say in this format abc abs ajfhg agjfh fhs ghg jhgjs fjhg dj djk djghd dkfdf .... .... ...... i want to read the first line at certain time for eg : at 10clk
0
10637
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10376
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10379
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10115
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9199
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5687
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4332
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3861
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3014
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.