473,748 Members | 2,887 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2148
On 2007-04-12, fl*****@technol ogist.com <fl*****@techno logist.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*****@technol ogist.com <fl*****@techno logist.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(b lock)
block = []
block.append(li ne)
if block:
blocks.append(b lock)

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
1824
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 and document generation tool http://interscript.sourceforge.net for all my programming tasks, both commercial and personal.
16
2906
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 loaded into cache, the slideshow doesn't look very nice. I am not sure how/when to call the slideshow() function to make sure it starts after the preload has been completed.
3
2614
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 as "gotchas") for doing this. I'm implementing a program that needs to read a standard configuration file, in key=value pairs for starters (though I'm open to other ideas). Basically it would be no more than about 20 lines total, one key per...
9
23606
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 time) b) fgets (read in blocks of whatever size) c) fread (get the size of the file and fread the entire thing into memory) and when would it be appropriate to use either a, b, or c?
2
5382
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 this works if the string is on a single line. If the string goes on more lines (caused by a new line or word wrap), it seems that I need to compute the space between the lines, since the height
11
2838
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 to a webpage where viewers can read the rest of the article: This is the first few characters of text from my file.......<a href="article-to-read.aspx"> MORE </a> ...I also need to do some in file parsing where I start at one known keyword (START...
13
4511
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 command set consisting of several single letter commands which take no arguments. A few additional single letter commands take arguments:
2
2589
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 looking for a single text stream that results from processing a file containing these directives. Even better would be an iterator(?) type
2
1909
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: http://www.websequencediagrams.com/examples.html For those that don't want to go to the link, consider the following, *very* simplified, example:
0
8991
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8830
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9544
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
9372
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
9324
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
6074
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4606
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3313
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
2783
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.