Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #85972

Re: Python - parsing nested information and provide it in proper format from log file

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; 'else:': 0.03; 'elif': 0.05; 'subject:Python': 0.06; 'detect': 0.07; 'nested': 0.07; 'none:': 0.07; 'remaining': 0.07; 'subject:file': 0.07; 'string': 0.09; 'friday,': 0.09; 'iterate': 0.09; 'logic': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:parsing': 0.09; 'python': 0.11; 'def': 0.12; '%r"': 0.16; 'bugs.': 0.16; 'count.': 0.16; 'dump': 0.16; 'increment': 0.16; 'index.': 0.16; 'iterating': 0.16; 'iteration': 0.16; 'line.split()': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:format': 0.16; 'subject:log': 0.16; 'variable.': 0.16; 'words.': 0.16; 'student': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'variable': 0.18; 'split': 0.19; '>>>': 0.22; 'import': 0.22; 'reset': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'comparing': 0.24; 'integer': 0.24; 'parse': 0.24; 'second': 0.26; 'header:X-Complaints-To:1': 0.27; 'tried': 0.27; 'idea': 0.28; 'thus': 0.29; 'words': 0.29; "doesn't": 0.30; 'code': 0.31; 'lines': 0.31; 'file': 0.32; 'probably': 0.32; 'another': 0.32; 'checking': 0.33; 'subject:from': 0.34; 'basic': 0.35; 'convert': 0.35; 'late': 0.35; 'test': 0.35; 'but': 0.35; 'there': 0.35; 'really': 0.36; 'consistent': 0.36; 'false': 0.36; 'doing': 0.36; 'should': 0.36; 'wrong': 0.37; 'area': 0.37; 'ends': 0.38; 'filter': 0.38; 'to:addr:python-list': 0.38; 'short': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'enough': 0.39; 'received:org': 0.40; 'how': 0.40; 'new': 0.61; 'matter': 0.61; 'first': 0.61; 'times': 0.62; 'email addr:gmail.com': 0.63; 'show': 0.63; 'name': 0.63; 'provide': 0.64; 'more': 0.64; 'total': 0.65; 'account': 0.65; 'here': 0.66; '20,': 0.68; '4th': 0.74; 'age': 0.80; 'counts': 0.83; '2015': 0.84; 'abc': 0.84; 'otten': 0.84; 'habit': 0.91
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: Python - parsing nested information and provide it in proper format from log file
Date Fri, 20 Feb 2015 15:27:59 +0100
Organization None
References <0097dab0-301c-42e1-a6be-b21eb5356567@googlegroups.com> <mailman.18920.1424437896.18130.python-list@python.org> <03cf1f25-b31b-4c48-96c9-be86a2ecdbc8@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p57bd94d3.dip0.t-ipconnect.de
User-Agent KNode/4.13.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.18922.1424442491.18130.python-list@python.org> (permalink)
Lines 147
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1424442492 news.xs4all.nl 2908 [2001:888:2000:d::a6]:50164
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:85972

Show key headers only | View raw


jt11380@gmail.com wrote:

> On Friday, February 20, 2015 at 8:11:59 AM UTC-5, Peter Otten wrote:
>> Jay T wrote:
>> 
>> >  have some log file which has nested data which i want to filter and
>> >  provide specific for student with total counts
>> > 
>> > Here is my log file sample:
>> > Student name is ABC
>> > Student age is 12
>> > student was late
>> > student was late
>> > student was late
>> > Student name is DEF
>> > student age is 13
>> > student was late
>> > student was late
>> > 
>> > i want to parse and show data as Student name, student age , number of
>> > counts how many times student was late e:g Name Age TotalCount
>> > ABC 12   3
>> > DEF 13    2
>> > 
>> > Please help me with solution that will be really grateful.
>> 
>> What have you tried? Please show us some code.
>> 
>> The basic idea would be to iterate over the lines and split the current
>> line into words.
>> 
>> If the second word is "name" and it's not the first iteration print the
>> student's name, age, and was_late count. Then set the name variable to
>> the new name and reset age and was_late to 0. To detect the first
>> iteration you can set
>> 
>> name = None
>> 
>> before you enter the loop and then check for that value before printing:
>> 
>> if name is not None:
>>     ... # print student data
>> 
>> If the second word is "age" convert the 4th word to integer and set the
>> age variable.
>> 
>> If the second word is "was" increment the was_late counter.
>> 
>> Remember that when the loop ends and the file was not empty you have one
>> more student's data to print.


> Now get stuck to get count for total_late time as it is part of name, age
> so how to write logic which counts as a part of group.
> 
> any help will be grateful.

Try to write code that does what I describe in my outline. Initialise name 
before the loop and dump the collected data when you encounter a new name.

name = None
total_late = 0
age = "unknown"

with open("student.txt") as instream:
    for line in instream:
        words = line.split()
        if words[1] == "name":
            if name is not None:
                print name, age, total_late
            name = " ".join(words[3:])
            age = "unknown"
            total_late = 0
        elif words[1] == "was":
            total_late += 1
        elif words[1] == "age":
            age = int(words[3])
        else:
            print "don't know what to do with line %r" %line
if name is not None:
    print name, age, total_late

Checking whole words has the advantage that there will be no match if the 
string "name" or "age" is part of the student's name.

> I tried to implent below code and got stucked how to do nested loop to
> count instead doing another logic and parsing:
> 
> import re
> def GetName(input_string):
>                   myName=input_string.split()
>                   myName1= myName[1]

That's the wrong index.

>                   return myName1
> def GetAge(input_string):
>                   myAge=input_string.split()
>                   myAge1= myAge[2]

That's the wrong index.

>                   return myAge1

In general you should test your functions independently from the whole 
program. That way you can build on known-good components and thus reduce the 
area where to look for remaining bugs.
                      
> file = open('mylogfile')
> log_data = file.readlines()

The file is probably short enough that it doesn't matter here, but iterating 
over the file directly is good habit to get into. Example:

with open("mylogfile") as log_data:
   for eachline in log_data:
       ...

> print 'entered'
> for eachline in log_data:
>            input_string = eachline
>            if 'name' in input_string:
>                     sometextval = GetName(input_string)
>                     print "name", sometextval
>            if 'Age' in input_string:

This test is problematic because Python takes case into account when 
comparing strings:

>>> "age" == "AGE"
False
>>> s = "RAGE"
>>> "age" in s
False

If case isn't consistent you should convert the string to lowercase:

>>> "age" == "AGE".lower()
True
>>> "age" in s.lower()
True

>                      sometextval2 = GetAge(input_string)
>                      print "Age", sometextval2
> 

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Python - parsing nested information and provide it in proper format from log file Jay T <jt11378@gmail.com> - 2015-02-19 18:42 -0800
  Re: Python - parsing nested information and provide it in proper format from log file Peter Otten <__peter__@web.de> - 2015-02-20 14:10 +0100
    Re: Python - parsing nested information and provide it in proper format from log file jt11380@gmail.com - 2015-02-20 05:31 -0800
      Re: Python - parsing nested information and provide it in proper format from log file Peter Otten <__peter__@web.de> - 2015-02-20 15:27 +0100

csiph-web