473,324 Members | 2,239 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

parsing text in blocks and line too

Goodmorning people :)
I have just started to learn this language and i have a logical
problem.
I need to write a program to parse various file of text.
Here two sample:

---------------
trial text bla bla bla bla error
bla bla bla bla bla
bla bla bla on more lines
trial text bla bla bla bla warning bla
bla bla more bla to be grouped with warning
bla bla bla on more lines
could be one two or ten lines also withouth the tab beginning
again text
text can contain also blank lines
text no delimiters....
--------------
Apr 8 04:02:08 machine text on one line
Apr 8 04:02:09 machine this is an error
Apr 8 04:02:10 machine this is a warning
--------------
parsing the file, I'll need to decide if the line/group is an error,
warning or to skip.
Mine problem if how logical do it: if i read line by line, I'll catch
the error/warning
on first and the second/third/more will be skipped by control.
Reading a group of line i could lose the order on the output: my idea
is to have
an output in html with the line in the color of the check (yellow for
warning,
red for error).
And i have also many rules to be followed so if i read one rule and
then i search
on the entire file, the check will be really slow.

Hope someone could give me some tips.
Thanks in advance

Apr 12 '07 #1
2 2127
On 2007-04-12, fl*****@technologist.com <fl*****@technologist.comwrote:
Goodmorning people :)
I have just started to learn this language and i have a logical
problem.
I need to write a program to parse various file of text.
Here two sample:

---------------
trial text bla bla bla bla error
bla bla bla bla bla
bla bla bla on more lines
trial text bla bla bla bla warning bla
bla bla more bla to be grouped with warning
bla bla bla on more lines
could be one two or ten lines also withouth the tab beginning
again text
text can contain also blank lines
text no delimiters....
--------------
Apr 8 04:02:08 machine text on one line
Apr 8 04:02:09 machine this is an error
Apr 8 04:02:10 machine this is a warning
--------------
I would first read groups of lines that belong together, then decide on each
group whether it is an error, warning, or whatever.
To preserve order in a group of lines, you can use lists.

From your example you could first compute a list of lists, like

[ [ "trial text bla bla bla bla error",
" bla bla bla bla bla",
" bla bla bla on more lines" ],
[ "trial text bla bla bla bla warning bla",
" bla bla more bla to be grouped with warning",
" bla bla bla on more lines",
" could be one two or ten lines also withouth the tab beginning" ],
[ "again text" ],
[ "text can contain also blank lines" ],
[ ],
[ "text no delimiters...." ]
]

Just above the "text no delimiters...." line I have added an empty line, and I
translated that to an empty group of lines (denoted with the empty list).

By traversing the groups (ie over the outermost list), you can now decide for
each group what type of output it is, and act accordingly.
Hope someone could give me some tips.
Sure, however, in general it is appreciated if you first show your own efforts
before asking the list for a solution.

Albert
Apr 12 '07 #2
A.T.Hofkamp wrote:
On 2007-04-12, fl*****@technologist.com <fl*****@technologist.comwrote:
>Goodmorning people :)
I have just started to learn this language and i have a logical
problem.
I need to write a program to parse various file of text.
Here two sample:

---------------
trial text bla bla bla bla error
bla bla bla bla bla
bla bla bla on more lines
trial text bla bla bla bla warning bla
bla bla more bla to be grouped with warning
bla bla bla on more lines
could be one two or ten lines also withouth the tab beginning
again text
text can contain also blank lines
text no delimiters....
--------------
Apr 8 04:02:08 machine text on one line
Apr 8 04:02:09 machine this is an error
Apr 8 04:02:10 machine this is a warning
--------------

I would first read groups of lines that belong together, then decide on each
group whether it is an error, warning, or whatever.
To preserve order in a group of lines, you can use lists.

From your example you could first compute a list of lists, like

[ [ "trial text bla bla bla bla error",
" bla bla bla bla bla",
" bla bla bla on more lines" ],
[ "trial text bla bla bla bla warning bla",
" bla bla more bla to be grouped with warning",
" bla bla bla on more lines",
" could be one two or ten lines also withouth the tab beginning" ],
[ "again text" ],
[ "text can contain also blank lines" ],
[ ],
[ "text no delimiters...." ]
]

Just above the "text no delimiters...." line I have added an empty line, and I
translated that to an empty group of lines (denoted with the empty list).

By traversing the groups (ie over the outermost list), you can now decide for
each group what type of output it is, and act accordingly.
>Hope someone could give me some tips.

Sure, however, in general it is appreciated if you first show your own efforts
before asking the list for a solution.

Albert
If groups have 0 indent first line and other lines in the group are
indented, group the lines

blocks = []
block = []
for line in lines:
if not line.startswith(' '):
if block:
blocks.append(block)
block = []
block.append(line)
if block:
blocks.append(block)

But if 0 indent doesn't start a new block, don't expect this to work,
but that is what I infer from your limited sample.

You can then look for warnings, etc., in the blocks--either in the loop
to save memory or in the constructed blocks list.

James
Apr 12 '07 #3

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

Similar topics

2
by: John Max Skaller | last post by:
please reply to skaller@ozemail.com.au (I don't read this newsgroup, and the reply-to address is fake) For some time now -- over 5 years -- I have been using the interscript literate programming...
16
by: Terry | last post by:
Hi, This is a newbie's question. I want to preload 4 images and only when all 4 images has been loaded into browser's cache, I want to start a slideshow() function. If images are not completed...
3
by: John Doe | last post by:
I've been doing some reading/research on parsing simple configuration files through C, and have heard various opinions on the matter. I'd like to solicit some opinions and design criteria (as well...
9
by: Mantorok Redgormor | last post by:
If I am parsing a config file that uses '#' for comments and the config file itself is 1640 bytes, and the format is VARIABLE=VALUE, is it recommended to use a) fgetc (parse a character at a...
2
by: Zanna | last post by:
Hi all! I'm in difficulty with this: I need to know the height of a text line that is written with Graphics.DrawString(). In theory I need just to get the Graphics.MeasureString() result. But...
11
by: .Net Sports | last post by:
In VB.net, I'm trying to do a couple of things in a couple of different blocks of code. I need to take the first 25 characters of a text file, then append at the end some ellipses and a MORE link...
13
by: Chris Carlen | last post by:
Hi: Having completed enough serial driver code for a TMS320F2812 microcontroller to talk to a terminal, I am now trying different approaches to command interpretation. I have a very simple...
2
by: python | last post by:
I'm parsing a text file for a proprietary product that has the following 2 directives: #include <somefile> #define <name<value> Defined constants are referenced via <#name#syntax. I'm...
2
by: =?ISO-8859-1?Q?Andr=E9?= | last post by:
Hi everyone, I would like to implement a parser for a mini-language and would appreciate some pointers. The type of text I would like to parse is an extension of: ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.